The relationship between exercise and mental health is complex. It's pretty clear that exercise does boost your mood short-term thanks to endorphins which help relieve pain and make you feel good, but exercise is also effective longer-term in the treatment of mental illness.
In one study, sedentary adults with major depressive disorder were assigned to one of four groups: supervised exercise, home-based exercise, antidepressant therapy or a placebo pill. After four months of treatment, patients in the exercise and antidepressant groups had higher rates of remission than did the patients on the placebo. Exercise, they concluded, was generally comparable to antidepressants for patients with major depressive disorder. There have also been studies that show exercise as a beneficial treatment for anxiety and schizophrenia.
There are various ways that exercise affects your mental health:
Exercising for 45-60 minutes at least 3 times a week will show some improvement within 4 weeks, but should really continue for 10-12 weeks in order to have the best mental health benefit. It can be hard to get started, but even smaller amounts of exercise are beneficial as well.