The word cake has long been used figuratively in allusion to the fact that the food it denotes is a delicacy. For instance, the English playwright John Day (1574-1638?) wrote the following in his satirical comedy The Ile of Guls (London, 1606):
I could fashion the bodie, of my discourse fit to the eare of my auditorie: for to cast Eloquence amongst a companie of Stinctards, is all one as if a man should scatter Pearle amongst the hoggish animals ecliped Swine: no I had paraphrasticall admonitions of all sortes; Some against couetous Landlordes, and that would I squirt awongst beggerlie Tennants: Some against Vsurers, and that would I throw in at Prison Grates amongst prodigall Banqroutes: Some against the pride of the Court, and that honies the eare of the Citizen: Some against the fraude of the Citie, and that’s Cake and Cheese to the Countrie: Some against Protestants, and that’s plumpes the lasie Catholicke against Papist and Protestant, and that fattens the rancke witted Puritand, against Papist, Puritand, and Protestant; and that tickles the eare of the luxurious Atheist.
Similarly, cakes and ale means lively enjoyment. It is an allusion to Twelfe Night, Or what you will, by the English poet and playwright William Shakespeare (1564-1616):
(Folio 1, 1623)
– Malvolio. My masters are you mad? Or what are you?
Haue you no wit, manners, nor honestie, but to gabble
like Tinkers at this time of night? Do yee make an Ale-
house of my Ladies house, that ye squeak out your Cozi-
ers Catches without any mitigation or remorse of voice?
Is there no respect of place, persons, nor time in you?
– Sir Toby. […] Dost thou thinke because thou art vertuous, there
shall be no more Cakes and Ale?
Often used ironically to convey mild disapproval, the phrase to take the cake means to be the prime example of a type, quality, etc. It appeared in American English with the noun in the plural. The earliest instance that I have found is from the Lexington Union (Lexington, Mississippi) of 2nd August 1839:
Big Peach.—We were presented with a peach that grew on the farm of Capt. W. H. Turner, two miles from this place, which measured eleven inches in circumference, and weighing three quarters of a pound—beat this and take the cakes.
On 10th August of that year, the same newspaper published the following paragraph:
Cotton.—We have in our office at this time, seven large and well grown cotton bolls, growing out from the main stem within two inches. This beats any thing we have seen—this cotton is from the plantation of Samuel D. Shackleford, Esqu. in the southern part of this county. We have been shown some that we thought hard to beat, yet this takes the cakes.
The allusion seems to be to cake as the prize in a contest, and it is often said that this phrase originally referred to the cakewalk, an African-American entertainment having a cake as prize for the most accomplished steps and figures in walking. But this seems unlikely, since the word cakewalk is first recorded more than two decades later, in the journal of Henry Edgar, a prospector; the entry for 3rd May 1863 tells what happened to him and his companions when they were being held prisoners in a Native-American village of Montana:
(from The Mammoth Book of the West: The Making of the American West (2012), by Jon E. Lewis)
Simmons tells us we are wanted at the medicine lodge; up we go. Bill says, “Ten o’clock, court now opens.” We went in, the medicine man sat on the ground at the far end; both sides were lined with the head men Red Bear and Little Crow, the two chiefs of the village, sat beside the medicine man. We were taken in hand by an old buck; in the center of the lodge there was a bush planted, – the medicine bush – and around and around that bush we went. At last their curiosity was satisfied and we were led out to Red Bear’s lodge and told to remain there. We had a good laugh over our cake walk.
(However, the fact that Henry Edgar uses the term figuratively implies the earlier currency of the literal sense.)
Although the synonymous variant to take the biscuit is considered British, it has been used in American English, and the earliest instance that I have found is from The Morning Star (Wilmington, North Carolina) of 2nd August 1879:
A Louisville reporter speaks of his town as “the pure city before which the Ohio river crouches and plays its music through the Falls and hurries on.” This, it strikes us, rather takes the biscuit.
On 7th August 1888, The Athletic News and Cyclists’ Journal (Manchester, Lancashire) used an extended form of the phrase:
For a bit of steady batting, W. Farey’s performance in the match mentioned above, for Keighley, about takes the biscuit, box and all. He scored 72 runs, of which no fewer than 50 were got in singles. “More power to yer arm, sirrah,” applies here, surely?