when the "insanity" defense is raised:
First, there's the old M'Naghten rule, which holds that a person is legally insane if, as a result of his mental disease or defect, he either did not know that his act would be wrong; or he did not understand the nature and quality of his actions. Most states now follow the rule in the Model Penal Code, according to which a defendant is not responsible for criminal conduct "if at the time of such conduct as a result of mental disease or defect he lacks substantial capacity either to appreciate the criminality of his conduct or to conform his conduct to the requirements of the law."
Under either standard, proof of mere "mental illness," however defined, is not sufficient for an acquittal on the basis of insanity (or, in some cases, a finding of "guilty but insane," in which case the defendant usually goes to a mental institution, and if he ever recovers, is sent to prison to serve a sentence). To be legally insane you have to be so psychotic or delusional that you didn't really understand what you were doing. This is why the insanity defense is rarely successful. Many crimes seem to be so awful that we conclude that the perp must have been "crazy" to have committed them, and often that person is mentally ill according to the DSM, but they are not disturbed enough for a successful insanity defense. Jeffry Dahmer is a good example: Obviously he wasn't normal by any definition of the word, but he did not even raise an insanity defense because he clearly knew what he was doing and that it was wrong.
Because the requirements of the insanity defense are hard to meet, many people who commit terrible crimes that seem really incomprehensible probably are mentally ill, but are not treated as such by the law. Conversely, of course most people who are diagnosed as mentally ill in some respect are completely harmless.