Most of the time, genetic malformations. Most of the time, before 8 weeks.
But they can be also caused by incompetent cervix, by illness, by stress, by cord accidents, by nutritional deficiency, by organs growing improperly, by oxygen deprivation, by trauma... by lots and lots of different things. And it can happen at any point in pregnancy though obviously it is most common in the first trimester. 25% of all pregnancies end in miscarriage, but only 1% of pregnancies are miscarried after 12 weeks.
Usually the only thing that can really cause a miscarriage is a blunt trauma. Drugs. Incompetent uterus. Things like that. Stds. A lot of times they just happen. They usually happen in the first trimester