In general, a person who is accused of a crime must be able to understand the basics of the legal proceedings against them. If not, they will be found not competent to stand trial for the crimes of which they are accused. Courts typically try to determine whether defendants are competent by using a psychiatrist (or more than one psychiatrists) to examine the defendant and evaluate his or her competence.
The psychiatrists test the defendant for all sorts of disorders (both physical and purely mental) that could affect his or her competence to stand trial. The link below, for example, lists five separate broad types of disorders for which the psychiatrists might test the defendant. Please follow the link for detailed information.
Quick Answer: they plead for a mental competency test. if granted they take it. if then they have ann iq less than 70 than they are legally prohibited from being tried in court and placed in an insane asylum.