It depends on the jurisdiction but, very broadly speaking, the person might be ill such that:
they did not know what they were doing or that what they were doing was wrong (insanity)
their ability to understand their actions or make a reasoned decision or self control was highly impaired but not to the degree of insanity (diminished responsibility or perhaps provocation)
they were unconscious when they acted (automatism - e.g. an offence committed while 'sleep walking')
Clearly they lack the same culpability as a person with "good mental health", who consciously committed a criminal offence, knowing it was wrong.
That does not mean the ill person can 'get away with it'.
Claiming diminished responsibility as a defence to a murder charge may mean the person will instead be tried for manslaughter, which is also punishable by imprisonment.
Depending on the circumstances a court (and subsequent people in authority) might be persuaded that the person is so dangerous they must be indefinitely detained and treated without their consent.
Doesn't this sort of allow psychiatrists to make up the law, because
they can decide if something is considered a mental condition?
Just because a psychiatrist comes along doesn't mean the court will do what the psychiatrist says is best - the court will hear both sides of the argument, establish the facts, interpret the law and deliver a verdict.
Some people would argue that someone who would commit such crimes (such as rape) couldn't possibly be in their right mind.
Certainly, but so far as I'm aware that defence is very rarely used and never successful.