First, it is important to note that most scholars believe that Hamlet is not actually mad, and Hamlet himself explicitly says that he is ultimately in command of his actions. But many of the other characters in the play seem to believe he is mad. Polonius first raises the issue, claiming that the madness can be traced to Hamlet's unrequited love for Ophelia:
That he is mad, 'tis true: 'tis true 'tis pity;
And pity 'tis 'tis true—a foolish figure!
Ophelia herself bemoans what she views as Hamlet's descent into madness:
O, what a noble mind is here o'erthrown!
The courtier's, scholar's, soldier's, eye, tongue, sword, The expectancy and rose of the fair state,
The glass of fashion and the mould of form,
The observed of all observers, quite, quite down! ...That unmatched form and feature of blown youth Blasted with ecstasy.
Claudius is not completely convinced that Hamlet is actually mad, but fears him in any case, saying that "Madness in great ones must not unwatch'd go."
Furthermore, after her encounter with Hamlet, Gertrude tells her husband that she is convinced that he is "mad as the sea and wind when both contend which is the mightier." This is, to some extent, a ruse. Though Gertrude was very disturbed to see him speaking to the ghost of his father, which she could not see, she is also doing as Hamlet has instructed by portraying him as mad, and, perhaps, trying to shield him from culpability for the death of Polonius. It is unclear whether she actually believes him mad.