The length of time you can keep a cake depends on the type of cake and how you store it.
Most cakes that you make or buy can last for up to 2 to 3 days in the pantry or out at room temperature.
if you refrigerate the cake then you can usually keep a cake for 4 to 5 days before it goes bad and stale.
However if you take a cake and freeze the cake you can usually keep a cake in the freezer for up to 2 to 3 months.
I've heard that a fruit cake can last for up to 20 to 25 years in the freezer but most people would not keep a cake that long.
As long as you eat the cake within a few days of making it or buying it then the cake will be okay.
The longer you wait to eat or serve the cake the more stale it becomes.
So it's best to make cakes the same day or the day before so that it's at it's freshest.
If you're buying a cake it's best to buy the cake the same day or only 1 day before serving it.