I've always been confused, and fascinated, by the ghost. Why would a ghost in purgatory, who wants to go to heaven by sending his murderer to hell, ask his son to commit revenge, a pagan act, thereby sending him to hell as well?
And why does it appear to Hamlet? Most revenge ghosts appear to those that have murdered them. Banquo appears to Macbeth. Caesar appears to Brutus. Why, then, does the ghost appear to Hamlet and not Claudius?
And the ghost exhibits double standards. We are also forgetting that the ghost makes a deal with Hamlet: take revenge upon Claudius but leave Gertrude to heaven. Surely, the ghost would want Hamlet to take revenge on Gertrude too, if not for accessory to murder, then surely for incest.
And the ghost appears twice to Hamlet. Later in the closet scene, as Hamlet is getting rough with his mother, the Ghost appears again and tells him to back off. Freudian (psycho-analytic) critics have had a field day with this reprise of the Oedipal Complex.
I tend to think the Ghost as an instrument of performance. He has the best lines in the play. He's an agent of in medias res, who awakens Hamlet's performance, the same way Mercutio awakens Romeo. As critic Catherine England says:
Perhaps the ghost is a parallel to Polonius: a father sacrificing a child to a principle or a perceived greater good. The ghost doesn't reappear after Act III. Neither does Polonius. The functions of both are completed. Ophelia goes mad and dies. Hamlet, who was never mad, kills Polonius, comes to terms with death, and thus also with life, finally kills Claudius, and dies himself. Good and evil, life and death, married in one man, as is the true nature of mankind. Without the ghost, Hamlet could not reach that fulfillment of himself.
I agree with cadena. But this should be added: the ghost cannot be at rest until Hamlet revenges his "foul and most unnatural murder." Thus it falls to young Hamlet to give his father the peace he so sorely needs.
As we see immediately, this task falls heavily on Hamlet, and he sets out on a plan not just of simple revenge, not just the death of Claudius. No, Hamlet wants to kill Claudius when the king is at his most guilty and full of his own evil. This will be, in Hamlet's mind, the ultimate, perfect gift to a father he most loves and reveres.
This is how Hamlet wants to kill Claudius. This revenge will give his father his much-deserved rest (end of Act 4):
When he is drunk asleep; or in his rage;
Or in the incestuous pleasure of his bed;
At game, a-swearing, or about some act
That has no relish of salvation in't
Then trip him, that his heels may kick at heaven,
And that his soul may be as damn'd and black
As hell, whereto it goes.
Then Claudius will be in hell and his father will be in Heaven.
The main purpose of the ghost's appearance to Hamlet is to reveal the nature of his murder, and to prompt Hamlet to revenge. One of the first things that the ghost, who is Hamlet's recently deceased father, says to Hamlet is that he needs to
"lend they serious hearing to what I shall unfold...so art thou to revenge when thou shalt hear" (I.V.8-11).
He immediately tells Hamlet to listen closely, because he's going to relay some pretty serious information that will prompt Hamlet to revenge when he's heard it. So, that is the stated objective of the ghost. Also, being a murdered soul, he is filled with unrest, and cannot be at peace. He states that every dawn he goes to "sulphorous and tormenting flames" and is condemned for a certain time to "walk the night" (I.V.5, 14) until his deeds in his life have been atoned for. So another possible reason that he appears to Hamlet is that he is in a state of limbo between life and the afterlife, and cannot rest or move on. The castle is the life he knew, so, he haunts it, waiting for reprieve. However, the main purpose is to relay the information that his own brother has killed him, and that he wants Hamlet to defend his name and honor by getting revenge for that murder.
I hope that those thoughts help a bit; good luck!