This is because the ghost can choose who does see it and who does not, (during the night when it is doom'd to walk, that is). All of its appearances are purposeful. It is crucial not only to see its speeches, but also to mark the circumstances of its appearances in order to determine these purposes.
- In the first scene, the ghost's appearance becomes progressively real. As the guards, we want to understand what it is, and why it is. The most significant in this scene is its reappearance when Horatio, to some extent, accurately divines the reasons for its previous appearances:
"And even the like precurse of fierce events,
As harbingers preceding still the fates
And prologue to the omen coming on,
Have heaven and earth together demonstrated
Unto our climatures and countrymen."
This indicates to us (the audience) and himself that there might be some wisdom in Horatio's reasoning.
Therefore and moreover, the ghost's appearance was to alarm the guards in hope to meet Hamlet, who is certain to hear of it through them, and rightly, as Horatio promptly says:
"Let us impart what we have seen to-night
Unto young Hamlet. For upon my life,
This spirit dumb to us will speak to him"
Notice that, though the guards can also see the ghost, it only ever talks to Hamlet in the entire play. The ghost is not seen again (or at least does not feature again) after Hamlet has talked to it (until Act III), thus, perhaps, indicates that it no longer needs the guards to see it and that its initial appearances are deliberate.
- The reason why Gertrude cannot see the ghost is, again, because it does not wish her to. The purpose of this appearance is to scold at Hamlet for forgetting his given tasks:
"This visitation is but to whet thy almost blunted purpose." One scold is, as Hamlet rightly says, it comes to its
"tardy son to chide, that laps'd in time and passion, lets go by th' important acting of [its] dread command". The other scold, much more significant to your question, is because Hamlet was disobeying the ghost's command to leave his mother to the punishments of heaven by telling her the truth and asking her to repent.
In the first conversation with Hamlet, the ghost makes clear its disgust at the act of the "seeming-virtuous Queen":
"But virtue, as it never will be moved,
Though lewdness court it in a shape of Heaven,
So lust, though to a radiant angel link'd,
Will sate itself in a celestial bed,
And prey on garbage."
And, though her offence may not amount to the urgent punishment that the ghost asks Hamlet to give to Claudius, the anguished spirit of the King does not want to forgive her. If she was not confronted and advised of the terrible truth, she will not have the chance to repent and, thus, will have to face the consequences enforced by heaven (which she does deserve for her unknowing act of lust alone)(A similar reasoning is used when Hamlet decides not to kill Claudius whilst he is confessing.):
"Taint not thy mind; nor let thy soul contrive
Against thy mother aught; leave her to heaven,
And to those thorns that in her bosom lodge,
To prick and sting her."
So when Hamlet was very close to making Gertrude realise the unvirtuous nature of her new marriage and, even to persuading her that her new husband is a murderer, the ghost interferes simply to stop this from happening. If it lets Gertrude see it, the pale spirit of her murdered King, this would only further convince her that Hamlet is telling her the truth, and further amount to her remorse and penitence, which would be completely against its cause. But by not letting Gertrude see it, yet conversing with Hamlet at the same time, it, perhaps wittingly, aids Hamlet on his task of faking insanity. Therefore, this invisibility is purposeful. And, therefore, who does see the ghost and who does not is intentional on the part of the ghost.
Perhaps the ghost must be cunning in this, for, though Hamlet appears to grief and obey his father's wishes whole-heartedly, his just and extraordinary sense of morality understands the immorals of these wishes and contemplates them with even more devotion.