A cavity, very simply is a hole. It is made by the bacteria of the contagious disease "caries".
In order to have a dental cavity we need four ingredients: a tooth, the right bacteria, food for the bacteria and some time for the bacteria to make the hole.

No Cavity Club
The bacteria responsible for caries thrive on sugars and other processed carbohydrates. They grow to form a complex biofilm of 600+ different species of bacteria. This takes time, 24 hours, just to get the bacteria organized. So, if you clean those bacteria off your teeth, your teeth are safe for 24 hours.

If left on the teeth, that biofilm of bacteria then generates acids that dissolve calcium out of the teeth, weakening the tooth structure and eventually making the hole. This takes months of time. During this process the affected area slowly turns chalky white as the enamel softens. As the white lesion progresses, it gets porous enough that stain from food or bacteria begins to penetrate and the white lesion darkens to brown.
Finally, the structure gets soft enough that pieces chip out and parts dissolve away leaving the hole. Note also, that before the enamel collapses the acids have penetrated to the softer inner portion of the tooth where the cavity grows faster. When the enamel finally collapses, we find that the cavity inside the tooth is much larger than the hole in the enamel. Cavities take time to develop, although the collapse of the enamel may seem like an overnight event.
