Cursed non-corpse food items, with the exception of the fortune cookie, are always rotten when you eat them. So the first course of action tends to be testing to see if it is cursed or not. See the top of Kaestur's answer to this question for details on that, but the simplest method is to drop it on the ground and see if your pet will walk on it.
Past that, it's primarily a game of luck. As with corpses, there is no visual indication of whether food is rotten. But unlike the deadly corpses, there is actually a check made when you eat the food. It is only made after it has been a certain number of turns since the generation of the item. So two rations of equal age can have different results when eaten.
Certain foods are more likely to rot than others. Eggs rot the fastest, to my knowledge. Fruit tends to rot earlier and more often than rations, although they are quite unfilling. The corpses of Lizards and Lichens never rot. To memory, lembas wafers don't rot over time, but if they do then they have a significantly lower chance of being rotten when eaten (and not cursed).