First, a cavity is a hole in your tooth. When bacteria is left on a tooth for a long time and fed sugar, the bacteria secrete acid. The acid actually dissolves the tooth, creating a hole in the tooth. It is then easier for bacteria to hide in the hole and continue to secrete acid, making the hole deeper and/or wider.
The outside of the tooth, the part you see and that everyone wants very white, is called enamel. It is very hard and can resist acid for a long time. It can also be healed with fluoride. Fluoride bonds with crystals in the enamel and makes the crystals grow. The crystals fill the hole, making it disappear or “heal”.