This expression is used to say that something is the strongest or most extreme example. Getting a free cake as a prize sounds like a good thing, but surprisingly, it is actually usually said about something bad.
He's done some careless things before, but losing his passport takes the cake!