The story
Attested since circa 1870. Originally used in reference to the winners of cake walk competitions in black communities of the southern USA in the 19th and early 20th centuries, who were said to have "taken the cake", which was oftentimes the prize.
Etymology from Wiktionary, CC BY-SA 4.0.