It is certainly understandable for someone who has just lost their father, and gained a stepfather to suddenly go mad. However, some time passes before Hamlet is "mad." In fact, before he even begins showing signs of madness, he says to his friend Horatio "As I perchance hereafter shall think meet to put an antic disposition on" (Act I, scene V, lines 166-167). It is not until after this statement that Hamlet becomes mad, and in saying this statement, it is implied that he is in fact feigning madness. Later, as Hamlet is speaking to Guildenstern, he makes the analogy that he is "but mad north-north-west: when the wind is southerly I know a hawk from a handsaw" (Act II, scene II, lines 408-410), again indicating that he is only shamming insanity. Also, in a heated conversation in which his mother is questioning his sanity, Hamlet says "I essentially am not in madness, But mad in craft" (Act III, scene IV, lines 168-169). Hamlet is putting on an act, a deceiving performance in order to confirm who was involved with his father's death.
Hamlet only performs his act for certain characters, though. Only in the presence of Gertrude, Claudius, Ophelia, Polonius, Rosencrantz, and Guildenstern does he behave as a madman. These are the characters whom Hamlet may have reason to suspect of a part in his father's death. In feigning madness, Hamlet confuses these characters, in hope of learning the truth of the murder of the king. In the third Act, Hamlet is set up to confront Ophelia, and promptly displays an antic disposition. He speaks in circles and contradicts himself plainly telling her "I did love you once" (Act III, scene I, line 118), and then in his next dialogue "I loved you not" (Act III, scene I, line 123). However, in different company, like that of Horatio, Bernardo, Francisco, The Players, and the Grave diggers, he is perfectly sensible. Even though his performance is a good one, Polonius notes that "Though this be madness, yet there is method in t" (Act II, scene II, lines 211-212), hinting that he is catching on to Hamlet's act. Polonius sees a reason behind the madness, giving credibility to Hamlet's act. The King Claudius is also suspicious of the reason for Hamlet's madness. After witnessing the meeting between Hamlet and Ophelia, he makes the decision to keep an eye on Hamlet, saying "Madness in great ones must not unwatch'd go" (Act III, scene II, line 200). He realizes that this madness that Hamlet is showing could be dangerous to himself, or his kingdom.
Though Shakespear never says what the method is behind Hamlet's madness, it is apparent that he is indeed feigning his sickness. He was visited by his father's ghost which tells him that he had been murdered by Hamlet's own uncle, and that he must take revenge. While Hamlet knows that his father was wronged, he must decide whether or not to avenge his death. Acting quickly, Hamlet assumes a role of insanity in an effort to confirm his suspicions about Claudius' involvement. He admits to other various characters that he is putting on an act, and only plays this act for certain characters, a couple of whom sense that there is a reason behind it. He is cruel to these characters that he can't trust, but as Hamlet himself says "I must be cruel, only to be kind: Thus bad begins and worse remains behind" (Act III, scene IV, lines 163-164).