Well to add a little more !!science!! to the thread.
Burying just one part of a dwarf in a coffin is enough to put them to rest. I had a dwarf get his arm chopped off and then die, he came back as a ghost. I let them bury the arm, but left the corpse forbidden and that was enough to put him to rest. Not sure if it has any practical value since we have slabs though.
Invading enemy dwarves (from goblin ambushes) can become ghosts. The two I got didn't hang around the body though, they both haunted my barracks (which is funny because they were both killed by caravan guards not my dwarves).
Dwarf liaisons can become ghosts too. In this case, he did haunt his body (standing literally on top of it most of the time).
Not sure if regular dwarf traders/guards can become ghosts or not. I suspect they can, since you bury them, but I've not had very many die and none became ghosts yet.
I doubt anything else can become a ghost, despite the fact you can make slabs for them. Regular goblins surely don't and human traders/guards don't seem to either. If I can get the humans to siege me I'll try killing a few weaponmasters/important civ figures and see if they come back but I doubt it. Elves probably don't even have souls at all, so I doubt they are even useful as post-mortem fortress guards.
I've not had any really harmful ghosts yet, so I can't say if they attack invaders or not.