It's important to note that some literary analysts don't think Hamlet's madness is an act but that he is literally driven crazy by his life which is spinning out of control on all fronts. Still, others do believe it's an act.
Hamlet is visited early in the play by his father's ghost, who asks Hamlet to avenge his murder by killing his brother and Hamlet's uncle, Claudius. Hamlet really doesn't know whether to believe the ghost for a while, and he stalls for time. In the meantime, Ophelia, whom he seems to have been genuinely close to prior to the play's opening, reports all Hamlet's "strange" actions to her father and Claudius. Additionally, Rosencrantz and Guildenstern, former friends of Hamlet, are employed by Claudius and Polonius to watch over Hamlet and report back to them. His mother has hastily married this same murderous uncle and goes so far as to tell Hamlet that he has "insulted [his] father" (III.iv.10).
Hamlet doesn't know whom he can trust besides Horatio. His world is full of potentially murderous, lying, backstabbing former friends and family. If he wants to fulfill the wishes of his father, he needs to buy some time to investigate the claims.
Hamlet is intelligent and talented in wordplay. Thus, he crafts a new image for himself so that all those employed to spy on him will be thrown off course in his true purpose: carefully investigating the ghost's claims about Claudius.
It works. Most of the characters are confused by Hamlet's actions, and Hamlet does discover that Claudius is the murderer that his father's ghost claimed. Thus, his actions of insanity show Hamlet's skill in navigating a world that he cannot trust in order to learn the truth.
Hamlet's biggest reason for pretending to be crazy is that he wants to be able to investigate the accusations made by his father's ghost against his uncle and new stepfather, Claudius, without raising any suspicion that he knows the truth about his father's death. It isn't particularly difficult for him to pretend insanity because he is already so grief-stricken by his father's death and disgusted by his mother's very hasty remarriage to a man who is, according to the Bible, her brother (since she was married to his brother). Further, once Hamlet learns that Claudius actually murdered his father, he adds anger to his mix of emotions. Then, to add insult to injury, he is abandoned by the woman he loves, Ophelia, when her father tells her she must break things off with Hamlet in order to preserve her virtue; thus, on top of Hamlet's grief, disgust, and anger, he is heartbroken. It is not difficult, then, for him to feign insanity, because he truly is experiencing so many painful emotions.
Hamlet pretended to be crazy for several closely related reasons. He pretended to be crazy to have freedom to examine Claudius's guilt, to find a way to do what the Ghost asked, to make people think he was no threat, to distract attention from his investigation into his father's death, and so he could say outlandish things without striking a nerve.
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,
... by pronouncing of some doubtful phrase,
As 'Well, well, we know,' ...
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. (I.v)