In Harry Potter and the Sorcerer's Stone, Nearly-Headless Nick and some other ghosts can visit the Hogwarts students, but why can't other deceased people - such as Harry's parents?
At the end of Order of the Phoenix, Harry asks Nearly-Headless Nick about the nature of ghosts, after the recent death of another character. This is the most detailed explanation in the series of how ghosts work, and who comes back as a ghost:
“Wizards can leave an imprint of themselves upon the earth, to walk palely where their living selves once trod,” said Nick miserably. “But very few wizards choose that path.”
“Why not?” said Harry. “Anyway – it doesn’t matter – he won’t care if it’s unusual, he’ll come back, I know he will!” […]
“He will not come back,” repeated Nick. “He will have… gone on.”
“What d’you mean, ‘gone on’?” said Harry quickly “Gone on where? Listen – what happens when you die, anyway? Where do you go? Why doesn’t everyone come back? Why isn’t this place full of ghosts? Why—?”
“I cannot answer,” said Nick.
“You’re dead, aren’t you?” said Harry exasperatedly. “Who can answer better than you?”
“I was afraid of death,” said Nick softly. “I chose to remain behind. I sometimes wonder whether I oughtn’t to have… well, that is neither here nor there… in fact, I am neither here nor there…”
He gave a small sad chuckle. “I know nothing of the secrets of death, Harry, for I chose my feeble imitation of life instead.”
— Order of the Phoenix, chapter 38, The Second War Begins
So only people who choose to remain behind will appear as ghosts. Presumably Harry’s parents are in the majority of people who choose to “go on”, as well as the person who’s just died.
There was an interview with JK Rowling shortly after the publication of Deathly Hallows which discussed the issue more succinctly:
If [Harry] could meet [his parents] as ghosts, that would be a much more meaningful interaction, but as Nick explained at the end of Phoenix—I am straying into dangerous territory, but I think you probably know what he explained—there are some people who would not come back as ghosts because they are unafraid, or less afraid, of death.
So Harry’s parents were less afraid of death, and so they chose not to stay behind as ghosts.
As to why they made this choice, it’s harder to say. The way Nick phrases it, makes it sound like most people would choose to go on rather than stay behind (so they’d choose it as the default), but it could also be their affiliation with Dumbledore:
“To the well-organised mind, death is but the next great adventure.”
— Philosopher's Stone, chapter 17, The Man with Two Faces
As with Harry, I think Dumbledore would have taught the Order of the Phoenix not to fear death, but to accept it as the next step. That’s why Harry’s parents went on, and that’s why many other characters in the series, who were also Order members, didn’t stay around as ghosts.
As Nearly Headless Nick said, most wizards did not want to become a ghost. From the wiki, it also says that
Sir Nicholas explained that only wizards who fear death are likely to become ghosts.
She obviously did not fear death, for she was ready for Voldemort to take her instead of Harry.
I think that Harry's parents were very smart, and thought, "Would I rather only see Harry for the rest of his life, or for eternity in the afterlife?" If they really loved him, I think they'd rather see him forever, instead of the last years of his life. Anyways, he'll have lots of time to tell them all about it.
Ghosts are the spiritual remains of people who feared death. From there we can assume that Harry Potter's parents, who fought to save him, did not fear death whereas Nearly Headless Nick did.
Let's take Harry's parents, one at a time.
First, there's mom.
Now, she used an ancient magic, whether unconsciously or not, to use her sacrifice, her DEATH in order to protect her son.
I don't know all the particulars of that, but I'm going to assume there's no take backs on that. And being a ghost is a bit of a take-back because, as Nearly Nick says (see alexwlchan's answer) "I am neither here nor there" meaning that he's neither alive nor completely dead.
Next there's dad.
And in his case, this may depend on how quickly one becomes a ghost.
I don't believe that it's instantaneous, I believe that there's time and a decision to be made. It's hard to tell, because, as far as I know there's nothing in the HP universe that I know of which tells us how quickly a ghost manifests after the body is killed. I would say that it has to vary. There are no stories of all the ghosts even showing up at their own funerals, are there? And funerals are generally at least a day after death.
If it isn't instant, in just a few minutes, James is joined in death by his wife, who may tell him of her sacrifice, perhaps in their own version of King's Crossing. That they would chose to go on together seems perfectly reasonable.
Fear of death is cited as far as the reason for Nearly Headless Nick, but there may, in fact be many reasons for choosing not to move on. I think that the spirit/person has to feel strongly, far beyond what's normal. Nick was deathly afraid of death. But there are other cases which seem stranger. Like the Bloody Baron--according to the story Helena Ravenclaw tells, he commits SUICIDE out of guilt for killing her (she doesn't say that she instantly turns into a ghost before he kills himself). He doesn't stay as a ghost out of fear of death, most likely. It sounds like he may be staying out of guilt. And Helena, were I to guess from the textual clues, always wanted to be thought of as more than her mother--but she died before she could be.
Plenty of people die and don't come back to raise their children though. The question is why not?
- You can't physically help them, and would be nearly helpless if they were threatened.
- If your sole purpose for being around is little Junior, that means you likely never leave little Junior's side. Who actually wants their parents around for everything? A parent with absolutely NO LIFE outside their kid generally messes that kid up in some way. Also courting and marriage are going to be weird if your mother-in-law can and does walk through walls at will.
- Your kid will learn death is optional and learn to fear it.
I think that the normal amount of wanting to protect and love your heirs isn't enough for a person to upset the natural order of moving on.
I think the desire or emotion motivating coming back has to be a little unbalanced and abnormal for it to take. The most psychologically well-balanced ghost we encounter in the HP world is Fat Friar, and I think that's only because we don't know much about him.
The teacher that's a ghost seems addicted to routine.
The Helena/Bloody Baron one is at least interesting because it does make me wonder if the ghosts came back separately, or if the Baron came back BECAUSE he knew Helena did. That could have some bearing on why a couple would leave together rather than staying with their child. I honestly think that Lily's sacrifice doesn't allow her to come back as a ghost, and that James would simply follow her.