Sometimes by accident. The original serotonergic antidepressants were antibiotics that happened to have MAO inhibition as a side effect. Initially developed to treat tuberculosis, doctors noticed the drugs had a stimulating side effect. A year later, a psychiatrist decided to trial them for depression, and found some success.
It's actually the other way around. They didn't have very serious theories about the etiology of depression back then, the only clue they had was from the pharmacology of these drugs, which led to the situation where for many years the leading theory on depression was that it was caused by low levels of monoamine neurotransmitters(serotonin, norepinephrine and dopamine). The main evidence for this initially was the fact that these drugs seemed to work, and that they increased the levels or effects of monoamines. This theory has since become discredited as we've learned more, although the connection between monoamines and depression is still strong. It's just not the whole picture.