In Hamlet, Shakespeare uses Hamlet's madness as a thematic plot device in keeping with the style of most revenge tragedy (The Spanish Tragedy, Ur-Hamlet). According to Enotes:
Typically, these works feature such themes and devices as a wronged revenge-seeker, ghosts, madness, delay, sinister intrigue, a play-within-the-play, torture, multiple murders, and the realistic depiction of bloody violence onstage.
The formula goes that feigned madness masks the intentions of the revenger. Hamlet, however, seems so captivated by the "mad Hamlet" role that he refuses to give it up and take up the "avenger Hamlet" one.
Also, Shakespeare adds dimension and shows contrast to Hamlet's feigned madness by characterizing Ophelia's real madness. The only downside to Hamlet's madness is that it makes him a villain in the sub-plot with Ophelia; she cannot understand his motivations and, after Hamlet kills her father, she commits suicide.
So, Shakespeare does not invent the revenger's feigned madness; Thomas Kyd did that. Rather, Shakespeare layers Hamlet's madness with unprecedented psychological and philosophical underpinnings which show that Hamlet's free will is so threatened by the corruption in Denmark that it drives him close to madness.
Not only that, but Shakespeare uses Hamlet's feigned madness as a form of satire against the society at large. While mad, Hamlet rants against against the hypocrisy of his mother, uncle, Polonius, and friends Rosencrantz and Guildenstern.
In all, madness is used as a tragic and comedic construct.
Hamlet is a good example of an Elizabethan "Revenge" play. One of the essential elements in a revenge play is that a character is often driven "mad" as he/she contemplates taking revenge.
In Hamlet, madness is both a part of the plot and a theme - what is real and what is an illusion. Hamlet pretends to be mad and his "acting" is so realistic, that we are not sure whether to believe he IS mad, or he is really faking it. Hamlet himself fears that maybe is really IS mad, since he has seen a ghost, and yet, he is not the only one who has seen the ghost.
Ophelia becomes made when Hamlet spurns her. Gertrude and Claudius believe Hamlet is mad first because of his father's death, and then later, because of his break-up with Ophelia, but the reality is none of these. What is the truth?