The main cause of cavities is the frequent consumption of processed sugary foods and beverages, and not brushing and flossing regularly.
Simply put, a cavity is a hole. Cavities are formed by acid produced by plaque, which is the accumulation of bacteria and minerals on teeth.
Bacteria ferment and break down sugars, producing an acidic byproduct.
With continuing acidity around the enamel, it erodes and breaks down the enamel forming a hole, causing permanent damage to the tooth.
Genetics
The anatomy of the tooth has grooves and pits. Some grooves and pits are deep and have shapes that trap plaque and food easily and make it difficult to clean.
These areas are highly susceptible to cavities.
The enamel is the outer layer of the tooth and is the hardest and strongest material of the human body.
Some people may have thinner enamel, weaker enamel, or enamel that is not formed properly, and this will leave teeth more susceptible to cavities.
Enamel is 99% inorganic material and 1% organic, while dentin is 80% inorganic and 20% organic.
This is the reason we see cavities balloon in size once they reach the dentin.
Environmental
Malnutrition and diet (acidic foods and drinks), acid reflux, infectious disease, medication causing dry mouth.
These factors can either weaken the tooth or promote growth of bacteria leading to cavities.
Oral hygiene
The best way to reduce cavities is by regular brushing and flossing.
Depending on your situation your dentist may have recommendations that can help reduce your risk.