The ghost is of the former King Hamlet, who was killed by his brother before he had a chance to confess his sins. Even though he is a ghost, he still must follow the royal protocol. He would not in life have conversed with lowly guards about a matter of this importance, nor would he after death. The guards would be unable to do anything about the murder and would probably not be believed even if they had the courage to tell someone.
Secondly, the ghost is not just haunting in general. Ghosts had very specific purposes in returning to earth. They only had a short amount of time between dusk and dawn to accomplish this purpose. Timeliness demanded that ghost avoid any go-betweens and go straight to the source. In this case, the purpose is revenge. Hamlet had a duty to avenge the death of his father, and this is what the ghost demands. The guards would not have been able to achieve this purpose.
When Hamlet learns from his father's ghost that his father was killed by his Uncle Claudius, Hamlet wants to keep this knowledge a total secret. He also wants to keep it a secret that the ghost has been visiting the castle and that he has had a long interview with the ghost. Shakespeare takes a long time in having Hamlet swear the guards to strict secrecy. Now presumably the Ghost doesn't reveal any information to the guards for the same reason that Hamlet refuses to tell them anything about his conversatioin with the Ghost and swears them to strict secrecy. Both Hamlet and his father's ghost assume it would be best if Claudius knew nothing about it. Claudius is guilty of murder and treason. He is naturally fearful and suspicious. If he had any idea that his brother's ghost was haunting the palace, he could assume it was there in order to get revenge. There is no way that Claudius could get rid of the Ghost, but he might assume that the Ghost would be revealing the truth to his son and asking him to take revenge. Hamlet knows that his life would be in danger if Claudius found out that he had been conversing with his father's ghost, which is why he swears the guards to secrecy and also why the Ghost also declines to give them any information whatsoever. The Ghost is there for the specific purpose of talking to his son--but he won't even tell the guards he wishes to see Hamlet.