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.
O, that this too too sullied flesh would melt,
Thaw and resolve itself into a dew,
Or that the Everlasting had not fixed
His canon 'gainst self-slaughter! O God, God,
How weary, stale, flat, and unprofitable
Seem to me all the uses of this world!
We start off with a bang. Or, considering that this is Hamlet we're talking about, maybe more like a whimper: he's moaning about how depressed he is over his father's death and mom's remarriage, and wishing that his "flesh" would "melt"—i.e., that he'd die.
History Snack! Elizabethans believed the human body was made up of four basic elements, called humors: phlegm, blood, yellow bile, and black bile. Hamlet seems to be suffering from what Elizabethans referred to as "melancholy," which was associated with too much "black bile" in the body. This state led to lethargy, irritability, distorted imagination, and so on. Basically, it sounds a lot like what we call "clinical depression" today. But since this is 1600 rather than the 21st century, he can't just take some Abilify; he has to plot (and delay) a murderous revenge.
Textual Note: Some modern editions of the play read "sullied flesh" instead of "solid flesh." This is because the first folio (published 1623) edition of the play (which reads "solid") is slightly different than the first quarto (published 1603) edition, which reads "sallied." Modern editors who prefer the first quarto reading update the word "sallied" to "sullied," as in "stained." Does it matter? Sure it does. Some editors and literary critics prefer "sullied" flesh because it suggests that Hamlet feels that he personally has been soiled, stained, or contaminated by his mother's incestuous relationship with his murderous uncle. Given how he seems to feel about sex, we'd buy that.