The ghost in Hamlet serves several purposes.
Shakespeare uses the ghost to introduce the supernatural into the play. It is through the ghost that information from beyond the grave comes to Hamlet and the audience.
The second purpose for the role of the ghost is to give Hamlet his direction in the play. The ghost is the spirit of his father the former king (Old Hamlet), who was murdered by his own brother Claudius. And it is this information that drives the plot along—in Hamlet's actions.
The ghost first appears to the guards, and when Hamlet hears of this, he walks the battlements, looking for the spirit who looks like his dead father. It is from the ghost that Hamlet learns that the Old King Hamlet was murdered, not bitten by a snake while sleeping (which is the story released to the "public.") Hamlet's life is further complicated in that his mother has married her former brother-in-law, and that instead of Hamlet being named king, Claudius has taken the throne.
The ghost charges Hamlet with the responsibility of avenging his father's murder. He reappears when it seems that Hamlet is losing his momentum. Hamlet hesitates because he is not certain if the ghost is truly his father or simply a emissary of the Devil intent on tricking Hamlet into losing his soul, which he would do if he killed Claudius: Elizabethans believed it was a sin to kill a king.
Without the presence of the ghost, Hamlet would never have questioned his father's death, and the plot would never have moved on.
In Shakespeare's Hamlet, the Ghost serves as catalyst for the plot. When he tells Hamlet that his father was murdered and charges Hamlet to get revenge, he ignites the action of the play. This is a revenge tragedy, and the Ghost starts Hamlet on his revenge.
The Ghost also contributes to Hamlet's predicament and his lack of certainty. Because the Ghost's identity is in question, Hamlet needs corroboration before acting on the Ghost's story. The killing of a king is no small matter. This leads to Hamlet's pretending to be "mad," and his setting up the play-within-the-play to prove to himself that the Ghost is telling him the truth. According to Elizabethan tradition, the Ghost could be the ghost of Hamlet's father, but he could also be a demon disguised for the purpose of bringing chaos to Denmark.
The Ghost, then, contributes to themes of revenge, madness, acting and seeming, and to the issue of why Hamlet waits or delays.
The Ghost also provides a concrete image of Hamlet's father, giving concreteness to the person Hamlet opens the play mourning for, and provides exposition by revealing details that occured before the opening.
Of course, with his later appearance in the play, the Ghost also, among other things, and perhaps inadvertently, draws attention to Hamlet's obsession with his mother. Hamlet is at least as upset with his mother's hasty remarriage as he is with his father's murder. Hamlet is obsessed with his mother's sexual relations with Claudius. And the Ghost rebukes him for it.