In the play, Hamlet says that he is only crazy when he wants to be. He warns Horatio that he should be prepared to see Hamlet pretend to act crazy, and not to act in an unusual way when this happens.
Hamlet shares his thoughts, allowing that if he acts crazy, it will give him more leeway to discover if his uncle is truly guilty of killing Hamlet's father, Old Hamlet. The ghost of his dad has asked Hamlet to avenge his death, and Hamlet struggles with his own lack of action. However, if the ghost is not his father (but a servant of evil) and he wrongfully kills the King, Hamlet will lose his immortal soul.
So Hamlet tries to deal with his father's death, keep Ophelia at arm's length because he believes that as Polonius' daughter (who is loyal to the King) that she cannot be trusted; he misses his father and greatly resents his mother's remarriage. [During the time the play was written, Elizabethans believed that when a couple married, they became as one. When Old Hamlet dies and Gertrude marries and sleeps with Claudius (her brother-in-law), that part of her that is Old Hamlet is sleeping with his brother--Elizabethans believed Gertrude was committing incest by sleeping with Claudius.] Hamlet is also being followed and spied on by two guys from school (Rosencrantz and Guildenstern), and Claudius becomes more and more suspicious.
Hamlet has more than enough to make him crazy, but I think that while he acts erratic at times and impulsive (killing Polonius without being sure who was behind the curtain), he is not insane. He mourns deeply for Ophelia, and makes peace with his mother. Hamlet is a man whose life has been destroyed by his uncle's deceit and murder of Old Hamlet. He is angry along with everything else, but I don't believe he is insane: though if anyone had the right to be, Hamlet would be that man.