Traditional Japanese beliefs state that every human being has a soul called reikon. After death, the reikon exits the body and enters a state of purgatory, where it awaits the body's funeral and final rites. If completed properly, the reikon becomes satisfied and meets peace and becomes a protector of the family from which it came.
However, if a person dies an unnatural death or the final rites aren't properly conducted, the reikon becomes a yurei, or dim spirit. The yurei anguishes in purgatory wondering why someone just can't bury the damn body already. If the yurei develops strong enough emotional attachments to the physical world, it returns as a ghost.
In Japanese mythology there are many types of such spirits. These five are not only common in Japanese folklore but also closely resemble Western ideas of what a restless spirit is.
Onryo are spirits that have returned to the physical world to seek vengeance against those who treated them poorly while they were living. Almost always depicted as women, onryo were often mistreated by their would-be lovers during their lifetimes. However, their gender rendered them powerless to stop mistreatment. By becoming spirits they find their strength.
The Japanese spirit world is structured with the spirit world itself and the physical world on opposite ends of a spectrum, with a middle-ground purgatory creating a gap between them. The purgatory is essentially a place of reflection where spirits linger until they accept their fate and move on to the spirit world. However, those with strong emotional attachments to the living can cross the gap and return the physical world. In the case of onryo, the emotions are hatred or sorrow, and they only return to rest when they have their revenge, which is always justified.
Stories of onryo often involve cheating husbands being haunted but indirectly harmed; usually the spirit will wreak havoc on the village where their lover lives or kill their new lover to right themselves. These ghost stories used as cautionary tales against bad husbands, though given the number of these stories they probably weren't effective at deterring any on-the-side-boning.
Ubume can be translated to mean “childbirth woman.” These spirits are those of mothers who died during childbirth or before they could properly take care of their child. Given their grim origins, the stories revolving around these spirits are more sorrowful than those of onryo and focus on the mother's quest to ensure her child's safety after her own death. They cross purgatory through both their love and fear.
Generally there are two types of Ubume stories. The first involves the spirit directly interacting with her child after death, serving as a sort of guardian angel. There's no haunting to be had in these stories. However, in her providing for the child, the mother inadvertently leaves clues to her deceased nature behind. The most common are coins left for their child which turn into dead leaves after they've been exchanged.
The second type of the story sees the mother's spirit attempt to find a stranger and guide it to her baby so it can be raised up in society by someone who isn't ethereal and creepy. Stories of ubume carry the moral that a mother's love never ceases, nor is her job ever truly done.
In medieval Japan, it was a common belief that one's social status carried over with them into the spirit world. The more powerful someone was, the more likely it would make them a ghost capable of great destruction, so continuing to respect the dead was very important. Of course, if some douche-hat assassinated you for political gain, it didn't really matter if a shrine was built in your honor because you were going to be pretty pissed off regardless.
Goryo are “honorable spirits” of the aristocratic classes who had been wronged during their lives, making them a vengeance ghost similar to onryo. Unlike onryo, these ghosts weren't powerless house wives but powerful lords, courtiers and other political figures whose powers as spirits translated into natural disasters and catastrophes, capable of causing floods or flash fires and killing hundreds of people at a time, innocent or not. However, the rampage of a goryo didn't necessarily end with the death of those who wronged them. Only the supernaturally powered yamabushis could put these spirits to rest.