First off, no matter the corpse, never eat unless you just made that corpse or you have a method of preservation (tinning, icebox). Rotten corpses cause food poisoning (unless you're a fungus or a ghoul), and it doesn't take very long for a corpse to rot. And there isn't a visual indication of rotting, either, so the safe bet is to never eat something you didn't just kill. Most undead corpses are automatically rotten, as well. (See below for a more technical explanation.)
If the monster has poison or acid attacks, or is made of poison or acid, then there's a very high chance that you'll be poisoned or take acid damage. Both of these can be fatal, the former even instantly so, but poison and acid resistance will protect against this. Note that most poisonous corpses will chance to give you poison resistance, so this can be something to risk if you have enough health. Other elemental effects like fire, cold, and electricity are traditionally safe.
If the monster causes you to be stunned, hallucinating, or confused by natural attacks (as opposed to casting a spell), then it is highly likely you will be tripping after you eat its corpse. Some of these are very obvious. Actually, most creatures with negative effects like petrification will also carry over that effect with their corpse.
While it won't directly kill you, the corpses of teleporting creatures like imps, nymphs, tengu, and leprechauns will often give you teleportitis. Note that tengu do have a chance of giving you teleport control, so it's a good idea to start eating those when you do get stuck with teleportitis.
Unless you are of the Caveman profession or the Orc race, you are not a cannibal. This means that eating a member of your own race will give you a penalty in the form of the "Aggravate Monster" intrinsic (which is as bad as it sounds) and a penalty to your Luck. This penalty will also show up for non-Caveman, non-Orcs when eating any domestic dogs and cats (the line of creatures you can get as a starting pet, generally). Horses are fine to eat.
For specific exceptions, off the top of my head:
kobolds are always poisonous but never grant resistant.
Bats cause stunning.
Wraiths are safe to eat despite being undead. All adult
Dragons are safe to eat, as well as most babies, but baby green dragons are poisonous unlike their adult counterpart. Lichen and Lizards do not rot over time and carry no negative effects that some other members of the same creature class might, so they make excellent backup food.
The rotting process is a little bit complicated and based on luck more than anything. Every 10 to 29 turns, a corpse gains a degree of rottenness. For a blessed corpse, 2 degrees are subtracted, while for a cursed one 2 degrees are added.
When it reaches a rottenness of 4, it is considered old. When you eat an old corpse, there's a 20% chance you will lose 1-8 hp, and a 1/7 chance of it being rotten (as in rotten food, not deadly.) When it reaches a rottenness of 6, it will give you fatal food poisoning, killing you in 11-19 turns.
So, put more simply, a corpse is definitely safe to eat in all cases for 32 turns, and it can cause food poisoning after a minimum of 48. Everything beyond that is just random chance.
Undead corpses start out with an age of 100 turns, which is why they are almost always already rotten.
Attributions for information provided within comments to:
- WillfulWizard for reminding about teleporting monsters.
- ire_and_curses for corrections on non-rotting lizards and poisonous baby green dragons.
- Kaestur Hakarl, who investigated and discovered the safe measures for cannibalism.