A person who isn't competent to stand trial can't be convicted of a crime. Courts require competency before defendants stand trial in order to preserve due process—that is, to make sure the proceedings are fair.
(This article is about competence to stand trial; for information about a related topic, see What is the standard for determining whether someone is competent to plead guilty?)
Defendants have an unassailable right to understand the proceedings against them and assist in their own defense. If they're incapable of understanding and assisting, they're legally incompetent.
No matter how clear the evidence of guilt is, mentally incompetent people can't be convicted. A defendant might have shot someone in broad daylight, then confessed to the crime—if that defendant isn't competent, criminal proceedings must wait. Authorities will arrest and hold the defendant in custody and the prosecution will file criminal charges, but the case can't advance until the defendant's competency is "restored." (See What happens if a defendant is judged "incompetent to stand trial"?) The defendant can't "waive" the issue of fitness to stand trial—the law requires competency before a case can proceed.
Competency to stand trial is legally unrelated to the defendant's mental state at the time of the alleged crime. In other words, the issue of competency relates to the defendant's state of mind during criminal proceedings, not during the commission of the crime. In the example above, suppose that the defendant suffered from severe mental illness when he shot the victim. If that illness prevents him from understanding the subsequent criminal proceedings, he's incompetent and the proceedings must halt. Once he's received enough treatment to understand what's going on, he's competent—at that point the case can carry on. Whether he has a mental state defense to the crime, such as insanity or diminished capacity, is an issue to be determined at trial.
A court can find a defendant who has a mental illness diagnosis fit to proceed as long as the illness doesn't rise to the level of incompetence. For instance, if a defendant is taking prescription medications to manage mental illness, the court may find competency and move the case forward. Further, if a psychological evaluation determines that medication could improve a defendant's mental state, the court can order that the defendant receive the medication.
The determination of whether a defendant is competent is left to the judge. The judge must decide competency before trial, as soon as reasonably possible after it comes into question. The prosecution, defense counsel, and even the court can raise the issue at any time. Competency usually comes into doubt when the defendant's behavior indicates a lack of understanding. In some states, if defense attorneys believe there is any question about competency they must ask the court to have the defendant evaluated.
When a legitimate question arises as to competency, the defendant has a right to a hearing to determine fitness to stand trial. All trial courts have authority to order psychological evaluations of defendants, and in many states, an evaluation is automatic once a party raises the competency issue. Judges are to give great weight to the results of an evaluation, but can consider other factors, too, like the defendant's demeanor in court. Among the points a court should consider are whether the defendant can:
A defendant's unintelligence, education level, language difficulties, and challenges communicating are generally insufficient to support a finding of incompetency.
If the court determines the defendant is able to understand the surroundings, receive and interpret information, and make decisions based on that information, it will likely find the defendant competent. Ultimately, competency is a notoriously low threshold—a defendant need not have a sophisticated understanding of the case in order to be fit for trial.