First appears in literature in 1460 by John Russell's in his book The boke of nurture, folowyng Englondis gise, in a statement that translates to, "Cut the upper crust [of the loaf] for your sovereign". It was common to give a guest or the family members the best of the bread, meat and vegetables.
There's a wide gulf between that citation and the idea that only the aristocracy were given the upper crust of loaves to eat. The jump is that somehow the phrase then became shorthand for the nobility.
The term 'upper crust' didn't in fact come to be used figuratively to refer to the aristocracy until the 19th century. The earliest citation that can be found of the term with that meaning is in Slang: A Dictionary of the Turf, by John Badcock, 1823: "Upper-crust - one who lords it over others, is Mister Upper- crust."
The term had previously been used to refer to the outer crust of the Earth's surface and, more frequently, a person's head or hat. That latter use was still in use when the 'aristocracy' meaning was coined, as is shown by this entry from an edition of Grose's Classical Dictionary of the Vulgar Tongue, which was published in the same year as the above reference, 1823: ...but to hear it from the chaffer [mouth] of a rough and ready costard-monger, ogling his POLL from her walker [feet] to her upper crust [head].
Incidentally, costard-monger was the earlier name for costermonger - a street trader who sells greengrocery from a stall or barrow. A costard was the 14th century name for a type of large, ribbed apple and later came to be the name given to apples in general. A costard-monger was initially an apple-seller.
The 'Earth's surface' and 'head/hat' meanings connect 'upper crust' with 'top' and there's every reason to believe that our present application of the term to members of society is another use of that same metaphor. The connection between the 'upper crust' of society and the upper crust of loaves of bread may be more fantasy than fact.