Ghosts are a common element in revenge tragedy (which you can read more about by going to "Genre") so it's not terribly surprising that the specter shows up in the play. What is surprising is the way the ghost invites so much speculation.
What is the ghost? What does it want? Where has it come from? As Hamlet wonders, is it a "spirit of health or goblin damned" (1.4.5)? The answers to these questions are unclear.
Here's what the spirit claims: 1) The ghost says he's Hamlet's father (it sure looks like the guy); 2) The ghost also says that he was murdered by his brother, who happens to be Hamlet's uncle Claudius, the guy who's now married to Gertrude and sitting on the throne of Denmark; 3) The ghost also claims he's "doomed" to suffer in "sulph'rous and tormenting flames" until the "foul crimes done in [his] days of nature / Are burnt and purged away" (1.5.2-5). Sounds a lot like Purgatory (where sins had to be "purged" before a soul could make it to heaven).
OK fine, but there are a couple of hitches. First, purgatorial spirits weren't supposed to ask people to commit murder and that's what the ghost wants Hamlet to do. (Remember, he tells the prince he's doomed to suffer until Hamlet takes revenge on Claudius.) Second, Protestants don't believe in the doctrine of Purgatory and Hamlet is a Protestant. (He lives in Denmark, a Protestant nation, and goes to school in Wittenberg, where the Protestant Reformation began. Be sure to check out our discussion of "Religion" for more about this.) This makes the ghost a pretty suspicious figure, which is partly why Hamlet isn't so sure he can believe the ghost's claims. Hamlet's not about to go on a murdering spree until he knows the truth. So, while the ghost's appearance sets the revenge plot into motion, the uncertainty surrounding the specter is what delays the play's action.
A lot of literary critics notice that the ghost has a whole lot in common with young Hamlet. They talk alike (mostly about Gertrude's "unnatural" and "incestuous" relationship with Claudius) and they also kind of look alike at one point. Remember when Ophelia describes the way Hamlet appeared when he showed up in her room looking all ghostly "pale," almost "as if he had been loosed out of hell" (2.1.1)? Hell, as we know, is where the ghost may or may not have come from.
Some speculate that the ghost could be a figment of Hamlet's imagination. Even though other characters see the ghost (the castle guards and Horatio, for example), Hamlet's the only one who ever has a dialogue with it. It's also worth noting that Hamlet's the only character who sees or hears the ghost when it shows up in Gertrude's chamber to remind Hamlet to be nice to his mom (3.4.18). Has Hamlet been imagining his conversations with the ghost the whole time? Does this have anything to do with the fact that Hamlet says to Horatio "My father!--methinks I see my father [..] in my mind's eye" (1.2.12) before he even finds out that the ghost has been appearing on the castle walls? What do you think?
Regardless of whether or not we believe the ghost is "real," it seems pretty clear that the spirit's presence in the play dramatizes the way young Hamlet is emotionally haunted, as it were, by his father's memory. And why shouldn't he be? The prince has just lost one of the most important figures in his life, a man he idolizes and loves. Young Hamlet is also told by just about everyone he knows that he needs to move on and forget about his father – Claudius insists Hamlet's excessive grief is "unmanly" and Gertrude tells Hamlet to ditch his mourning clothes and to quit moping (1.2.2). Perhaps one of the simplest answers to the questions surrounding the ghost is that it stands for Hamlet's lingering memory of a lost but not forgotten loved one.