Baked food and Bacilus Cereus
One of the largest misconceptions in food safety is that safe food does not have bacteria. Safe food is, like all other food, always contaminated with bacteria. The difference is that safe food has been kept under conditions which have not allowed the bacteria to grow to dangerous levels. 

Cooking and baking both kills some bacteria, but does not sterilize the food. And even if it did, it gets recontaminated immediately anyway (exception: canned food in airtight containers). The difference in shelf stable food and perishable food is not that shelf stable food has no bacteria, it is that shelf stable food does not grow bacteria at room temperature. 

The items which you listed - bread, pies, etc. - simply don't have enough moisture to sustain a bacterial colony. This is what makes them shelf stable. Other shelf-stable foods are preserved in other ways, for example some kinds of pickle contain enough acid that bacteria cannot multiply in them. But there is nothing inherent in baking as a heating method that creates this result. If you fry some dough into a donut, it is also shelf stable. If you bake a casserole, it is not shelf stable and needs refrigeration. Depending on what you put in it, it will develop some pathogenic colonies if left outside, with B. Cereus a common possibility. 

If you want to think about food safety, you always have to think in terms of "which factor necessary for bacterial growth am I missing in this food". This is what prevents the most frequent misconceptions which people automatically come up with when starting from other assumptions.