Shakespeare goes to a great deal of trouble in the opening scenes to establish that it was indeed Hamlet's father's ghost the men saw on the battlements. In his day Shakespeare did not have any means of making the actor look "ghost-like," such as with luminous paint, so he has him dressed in armor to make him at least look somewhat strange and unique. It was essential to establish that this actor in armor is truly a ghost and truly Hamlet's father's spirit before Hamlet ever sees him. Shakespeare did not want to waste time when Hamlet encounters his father in having the father explain that he is really dead, etc. As Charles Dickens writes in A Christmas Carol:
If we were not perfectly convinced that Hamlet's Father died before the play began, there would be nothing more remarkable in his taking a stroll at night, in an easterly wind, upon his own ramparts, than there would be in any other middle-aged gentleman rashly turning out after dark in a breezy spot—say Saint Paul's Churchyard for instance—literally to astonish his son's weak mind.
The audience, too, must be convinced that the bearded actor in armor is Hamlet's father's ghost. With Banquo's ghost in Macbeth and Caesar's ghost inJulius Caesar, Shakespeare had no such problem, because everybody has seen both men getting very thoroughly murdered. But the ghost in Hamlet is a different case. Hamlet's father has been dead for at least several months, and nobody except Claudius saw him being killed. Shakespeare did not want anybody, including Hamlet, and especially his audience who are just witnessing the first act, thinking that this was the former king himself who had somehow recovered and was very much alive.
But dressing the ghost in armor to make him look somehow "ghost-like" raised other problems. Why armor? The explanation is somewhat complex. Shakespeare wanted the ghost's revelation about his murder by Claudius to come as a big surprise, not only to Hamlet, but to the entire audience. If the ghost had been dressed in ordinary clothing, or in a robe, or whatever, the audience might suppose that he had come to give his son a personal message. And they might further suspect that it concerned Claudius and Gertrude. The armor is a red herring. It is intended to make everybody believe that the ghost is concerned about the threat of war between Denmark and Norway. The ghost is dead. He cares nothing about the problems of the living. As Macbeth says of another murdered monarch:
Duncan is in his grave,
After life's fitful fever he sleeps well.
Treason has done his worst; nor steel nor poison,
Malice domestic, foreign levy, nothing
Can touch him further.
The dispute with Fortinbras is a flash in the pan. It is all settled by messengers. It was only intended to mislead Horatio, Marcellus, Bernardo, Hamlet, and especially the audience, into thinking that the ghost is concerned about an impending invasion. But by the time Hamlet meets his father's ghost in Act 1, Scene 4, he and the entire audience are thoroughly apprised and convinced that this is indeed the ghost of the dead King Hamlet.
Since Shakespeare invented a dispute between Fortinbras and the Danes, he characteristically must have decided to make further use of it as a sort of subplot; and at the very end of the play Hamlet nominates Fortinbras to be the new king of Denmark.
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.
There should not be the slightest doubt that the Ghost is a real ghost and that it appeared to Hamlet and had the interview with him as recorded. The reason for this is that Hamlet is a play and the entire theater audience also sees the Ghost and hears and sees everything that goes on between him and Hamlet. If the Ghost is a figment of Hamlet's imagination, then he must be a figment of ours in the audience as well--a mass hallucination! The Ghost is a real ghost. He is even listed in the cast of characters as "Ghost (Hamlet's father, the former King)." He is every bit as real as Claudius or Gertrude or Hamlet himself. I believe I once read that Shakespeare played the part of the Ghost in some of the productions. In Hamlet's case there are four other characters who also see the Ghost. Can Bernardo, Francisco, Marcelllus, and Horatio be imagining it too? Horatio ought to be considered a reliable observer. He reports the sightings to Hamlet and describes the Ghost in detail. He says:
I knew your father:
These hands are not more like.
People have asked the same question about Banquo's ghost in Macbeth. Is it real? Or is Macbeth only imagining it? It is positively a real ghost and everybody in the theater audience sees it along with Macbeth.
The play makes clear that those who see the Ghost see him/it as they remember King Hamlet as he lived. So the appearance of the Ghost is the product of individual characters' memory including Hamlet.
As for the Ghost speaking to Hamlet, the guards in Act 1 Scene 1 (and as recounted to Hamlet in 1.2 by Horatio) come to the conclusion that the Ghost wants to speak but will not speak to them. Horatio is convinced that the Ghost will speak to Hamlet. The consensus at the end of the first scene is that they will take this matter to Hamlet.
In Act 1 Scene 5 it is the Ghost that delivers the wealth of information to Hamlet. Of particular note is that the Ghost's message is a surprise to Hamlet. Also, the information is of a particularly personal nature that only a Ghost of the dead King Hamlet would know. That Hamlet later questions the veracity of the spirit, there is no question that Hamlet both saw and heard the Ghost.