The problem with trying to determine whether Hamlet is insane or not in this wonderful tragedy lies in the way that he reveals to Horatio and Mercellus that he actually intends to put on an "antic disposition" as he calls it after confronting the Ghost and discovering the "truth" about his father and who killed him. This does suggest that the scenes where Hamlet appears to be mad are mere disimulation. For example, when he appears to Ophelia in her chamber, as reported to Polonius in Act II scene 1, she certainly thinks he is mad based on his appearance:
Lord Hamlet, with his doublet all unbraced,
No hat upon his head, his stockings fouled,
Ungartered, and down-gyved to his ankle,
Pale as his shirt, his knees knocking each other,
And with a look so piteous in purport
As if he had been loosed out of hell
To speak of horrors, he comes before me.
Based on what Hamlet has just revealed in the previous scene, we can view this as a deliberate strategy to use Ophelia, who he clearly suspects will go running to Polonius who, in turn, will tell the King, to spread concern about his mental state. Appearing to be "mad" is therefore something of a stratagem for Hamlet whilst he considers what to do next and how to avenge his father.
However, at the same time, it is possible to look at the text and determine that there are moments when Hamlet is genuinely mad. Certainly, the above quote is poignant when it talks about his expression reflecting unspeakable "horrors." That this occurs just after meeting the Ghost could indicate that confronting the Ghost and finding out the truth about his father has mentally unhinged Hamlet. There is never enough evidence to clearly state that Hamlet has either become insane or he is just pretending.