system
Given the following post, respond as if you're commenting on Reddit:
human
Why do products like eggs, milk, butter, cream cheese spoil if left out of the fridge, but once baked into a cake, can be left at room temperature for days, or even weeks?

I thought it is because once they are baked, the bacteria is destroyed. But if I cook scrambled eggs and leave them out, they will rot pretty quickly.
gpt
One of the principal reasons is moisture. Generally speaking, bacteria, moulds and other microorganisms tend to reproduce very well in a wet or moist medium, and reproduce poorly in an arid one. Dry flour can be left out for months, but add a bit of sterile water to it, cover and let it sit out on the counter, and it becomes a science experiment, as all its included (and previously dormant) microflora sense the available moisture, become bioactive, begin to feast on its freshly dampened starches, and start reproducing like mad.  Likewise, consider the different results  obtained from leaving out jars of skim milk powder and liquid skim milk, or the olfactory difference between a kitchen sponge left sitting in the bottom of the sink for a few days, versus one that has been carefully wrung out and placed on an absorbent clay plant saucer after each use. 

By driving out most of the water with heat, the cook creates a more challenging environment for micro-organic growth, even if the finished dish is not rendered entirely dry. Your room-temp scrambled eggs may go bad pretty quickly, but that is because at a microbial scale, they still contain oodles of moisture; the equivalent volume of dry powdered eggs will support far less bacterial growth over the exact same time period and temperature range. 

 Cooked carrots, on the other hand, will spoil far more quickly than raw ones, simply because cooking has caused the fibrous cellular skeleton to collapse, and has converted the root's overall woody structure into a dense, moist mush in which newly-split off bacteria may more easily travel and spread, instead of being limited to competing with their new twin for the same microscopic wet spot within a forest of dry, insoluble fibre.

Your cake example *will* indeed go mouldy fairly quickly, if it remains nice and moist in a plastic bag of minimal volume. Anybody who goes on a rainy 3 day hike in the Pacific Northwest, only to discover how quickly blue fur will start to grow on their beef jerky, learns the importance of keeping even food that usually requires little or no refrigeration as dry as possible. After all, we don't refer to basic foodstuffs like sugar, oatmeal, cornstarch and flour as *dry* goods without a reason.

Other reasons for the slower spoilage of many cooked foods versus raw include: the high efficiency of heat at sterilizing food by killing the bacteria already contained within it, meaning more bacteria must find and colonize it before spoilage sets in with a vengeance; its denaturing effect on natural proteins; and its ability to convert starches into sugars, which at sufficient concentrations will literally suck the moisture out of any remaining bacteria via osmosis.  That's one of the reasons unrefrigerated honey simply doesn't go bad.