Clues in Hamlet

From the moment the ghost appears, Shakespeare offers clues as to its provenance which would have sounded alarms for the informed audience of his time. Here are some examples.

When Horatio is delegated to address the ghost because of his education, he speaks the following words:

What art thou that usurp'st this time of night,
Together with that fair and warlike form
In which the majesty of buried Denmark
Did sometimes march? By heaven, I charge thee, speak

The ghost appears offended at these words and exits the stage. Eleanor Prosser, in her book on Hamlet and the ethics of revenge, speculates that he is offended by the invocation of heaven.

The most obvious explanation is that the spirit is offended only when Horatio charges it "by heaven" to speak. ... It is "offended" by the name of Heaven and swiftly retreats.  --Eleanor Prosser, Hamlet and Revenge, Stanford Univ. Press, Stanford, 1971. p.98f.

The episode firmly establishes one point: this Ghost is forced to leave when Heaven is invoked. Horatio follows the warnings of religion. He charges the Ghost in the name of Heaven to identify itself, and it took no pious scholar to know that only demons would be "chased" by the invocation of God.  -- p.119

In its second appearance, the Ghost is again driven off; this time at the crowing of the cock. At this point, Marcellus clearly assumes this ghost is demonic and states that evil spirits cannot remain abroad upon hearing this symbolic voice of Grace.

It faded on the crowing of the cock.
Some say that ever 'gainst that season comes
Wherein our saviour's birth is celebrated
The bird of dawning singeth all night long;
And then, they say, no spirit can walk abroad,
The nights are wholesome; then no planets strike,
No fairy takes, nor witch hath power to charm,
So hallowed and so gracious is the time
. (I.i.152-164)

The coming of the dawn, and its ability to dispel walking spirits of evil is a part of cultural mythology that Shakespeare incorporates in his work.

And yonder shines Aurora's harbinger,
At whose approach, ghosts, wandering here and there,
Troop home to churchyards: damned spirits all.
 (A Midsummer Night's Dream)