The issue of Hamlet's madness in Shakespeare's Hamlet is complex to say the least. The more central issue is whether Hamlet is "mad" or is just faking it. But if one assumes he's mad and just wants to know what the cause of that madness is, the issues are a little clearer. First and most importantly, if Hamlet is mad it is due to grief over the death of his father/king. For example, Claudius and Gertrude are aware of his depression early in Act I and exhort him to recover from the loss. "How is it that the clouds still hang on you?" (I.ii) asks Claudius. Gertrude follows:
Good Hamlet, cast thy nighted color off,
And let thine eye look like a friend on Denmark.
Do not for ever with thy veiled lids
Seek for they noble father in the dust. (I.ii)
Soon after, still in scene two, Hamlet gives his famous "too too sallied flesh" soliloquy, and his depression becomes even more evident. He, in fact, wishes the "Everlasting had not fixed/His canon 'gainst self-slaughter." Hamlet is suicidal due to his father's death. One could cite numerous other examples of Hamlet's madness being due to grief over his loss.
Other issues that one could study are also present in the play: Polonious suggests Hamlet's madness results from Ophelia's rejection of him and Hamlet certainly suffers due to his perceived inaction, just to name two.
Hamlet has decided to pretend to be mad so that he can disguise his true intentions to avenge his father's murder by killing Claudius. In Act I Sc.5 after his father's Ghost has revealed to him how he was killed treacherously by his brother Claudius and after it had extracted a promise from him to avenge its murder Hamlet meets his two bosom friends Marcellus and Horatio. He confirms to them that the ghost is an "honest ghost" and swears them to secrecy. Then he tells them both that he is going to pretend to be mad, to "put on an antic disposition,"
Here, as before, never, so help you mercy,
How strange or odd soe'er I bear myself,
As I perchance hereafter shall think meet
To put an antic disposition on,
That you, at such times seeing me, never shall,
With arms encumber'd thus, or this headshake,
Or by pronouncing of some doubtful phrase,
As 'Well, well, we know,' or 'We could, an if we would,'
Or 'If we list to speak,' or 'There be, an if they might,'
Or such ambiguous giving out, to note
That you know aught of me: this not to do,
So grace and mercy at your most need help you, Swear.
Hamlet has decided to pretend to be mad for his own safety and security so that he can plan secretly to murder Claudius without raising the suspicions of those around him.