The section of Shakespeare's Hamlet that seems to prove that the Ghost is not a figment of Hamlet's imagination comes when Hamlet arranges for the traveling actors to present a scene just like his father's murder, as described by the Ghost.
When the Ghost first appears in Act One, he tells Hamlet that he was murdered:
List, list, O, list!
If thou didst ever thy dear father love—
…Revenge his foul and most unnatural murder. (I.v.26, 28)
The Ghost goes on to explain that while he slept in the orchard, a daily habit known to all in the castle, Claudius (the King's brother) murdered him, though all believe it was a snake's bite that took his life:
Now, Hamlet, hear.
'tis given out that, sleeping in mine orchard,
A serpent stung me. So the whole ear of Denmark
Is by a forged process of my death
Rankly abused. But know, thou noble youth,
The serpent that did sting thy father's life
Now wears his crown. (39-45)
Hamlet has been devastated by his father's death, and is disgusted with his mother's hasty remarriage to her brother-in-law. While he notes to his friends on the battlements that the Ghost is "honest" (telling the truth), he still wants irrefutable proof that Old Hamlet was actually murdered. (For if Hamlet kills Claudius and he is innocent, killing a king is a mortal sin, so the Elizabethans believed).
So when the players arrive at the castle, Hamlet gives them a scene to play that re-enacts the alleged actual events of the day when Old Hamlet died. If, Hamlet says, the Claudius does not react, Hamlet will know that the Ghost lied to him, and Old Hamlet was not murdered.
One scene of it comes near the circumstance,
Which I have told thee, of my father's death…
Observe my uncle. If his occulted guilt
Do not itself unkennel in one speech,
It is a damned ghost that we have seen,
And my imaginations are as foul
As Vulcan's stithy. (III.ii.72-73, 77-80)
The play begins and both Hamlet and Horatio watch Claudius as he watches the play. The actors play out the murder of one brother by another, to steal crown and queen. When the "brother" pours the poison in the King's ear, Claudius is clearly agitated and rises, bringing the play to an abrupt end. This is all the proof Hamlet needs, realizing at that moment that the Ghost had not lied to him. Had the Ghost's story been a lie, the Claudius would not have reacted at all.
In that the Ghost's description of his death seems to be exactly what drives the King out of the hall, we can be sure that Hamlet did see a ghost and it was not his imagination. It is important to remember, also, that Elizabethans believed in ghosts as well as witches, demons, elves, fairies, etc. The presence of the Ghost in the play would have been taken very seriously, and the audience would have been able to appreciate Hamlet's hesitation at first in completely believing in the Ghost's story without proof.