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 that this ghost isn't as straightforward as it seems.
What is the ghost? What does it want? Where has it come from? Is it a "spirit of health or goblin damned" (1.4.5)? And did someone remember to bring the ice?
We just don't know for sure. But 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). Hm, sounds a lot like Purgatory, where sins had to be "purged" before a soul could make it to heaven. (That also sounds like a no on the ice.)
But there are a couple of hitches. First, purgatorial spirits weren't supposed to ask people to commit murder, since that basically defeats the point of being purged of your sins. Still, that's exactly what the ghost wants. In fact, he says he's doomed to suffer until he gets his revenge.
Second, Protestants don't officially 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.) Pretty suspicious, if you ask us. Hamlet seems to agree, and he's not about to go on a murdering spree until he knows the truth. The ghost's appearance sets the revenge plot into motion, but it also 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)? Yeah, sounds a lot like the ghost to us.
So maybe the ghost-as-dad is just a figment of Hamlet's imagination. Other characters may see the ghost (the castle guards and Horatio, for example), but Hamlet's the only one who has a dialogue with it. He's also the only one 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?
Regardless of whether or not we believe the ghost is "real," we feel safe saying that the spirit represents the way young Hamlet is haunted by his dad's memory. We get it; the prince has just lost one of the most important figures in his life, a man he idolizes and loves, and everyone is just telling him 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 quit moping (1.2.2).
Maybe he's real and maybe he's not—either way, he sure seems real to Hamlet.