Despite what some would think, the insanity defense is very complicated and notoriously difficult to prove. A history of mental illness or a mental health diagnosis is not necessarily enough to be found not guilty by reason of insanity. To be acquitted of criminal charges on the basis of insanity, it's up to the defense to prove that the defendants did not know what they were doing or were unable to control their impulses.
The "irresistible impulse" test is used by the courts to show that the defendant was not able to control his or her actions as a direct result of a mental health issue. The lack of impulse control must be expressly related to the person's mental health to be a viable option. The M'Naghten Rule is another possible basis for an insanity defense, and it means that the defendant was unable to understand right from wrong because of a mental health deficit.
It's important to understand that even if the defendant's mental health is shown to be a contributing factor in the case, this does not automatically mean an acquittal. In some cases, the defendant will get a reduced sentence or be hospitalized in a mental health facility.
The insanity defense is only an option in a very small percentage of cases, and an even smaller percentage of those cases result in the defendant being found not guilty by reason of insanity. However, in any given case, there are normally several defense options, and a criminal defense attorney can help you understand each and how they may apply to your particular situation.
Source: FindLaw, "Insanity Defense," accessed July 08, 2015