A cavity forms when the top layer of the tooth (the enamel) is eaten away by acids. The acids are produced by bacteria (S.mutans and P.gingivalis) that lives in the plaque on our teeth and they feed on the food we eat – especially food with a high sugar content. They use the sugar in the food to produce an acid. The acid demineralises the enamel and a cavity is formed.
When the cavity is still very small and limited to the enamel, it can still be reversed by placing fluoride (from your toothpaste or mouth rinse) onto the tooth.
Our saliva also contains minerals (Calcium and Fluoride) that can repair the damage done by the acids. But, when the cavity continues to grow beyond the enamel and into the softer underlying dentin, we need to repair the cavity as soon as possible by placing a filling. When the cavity are not restored, it continues to grow till it reaches the nerve in the middle of the tooth. When this happens the bacteria enters the nerve canal causing an abscess (and a lot of pain!!). We then need to do either a root canal treatment (cleaning out and filling the nerve canal) or take out the tooth to treat the infection.
Even if we do a filling, you still need to change the environment in your mouth that caused the cavity in the first place. Otherwise the bacteria can eat away at the tooth structure around the filling and causing another cavity underneath it, and the filling doesn’t help. You can do this by brushing the area thoroughly (to prevent plaque build up) and also to avoid sugary foods that feed the bacteria.