Not all cooked food last longer than raw food. Most types of food have longer shelf life in their raw state. Take for instance rice, beans ,potatoes. But when you cook them without any form of preservatives, they tend to spoil easily. What matters is how you cook your food. Most food will decay because of water content. Bacteria thrives in warm moist environment. So if you are boiling and steaming, there's higher probability of decay because you are introducing liquid medium into the food but if you are roasting, grilling and frying,and the likes that do not involve liquids, these methods will allow the food to last longer since most of the water content will get evaporated by the heat.