Enamel consists primarily of a calcium-phosphate-based crystalline mineral called hydroxyapatite (HAP). [Arends et al, 1984] The HAP crystals are packed tightly together to form millions of microscopic prisms and lattices. Fluoride is incorporated into the solid crystal lattice by iso-ionic exchange to form fluorohydroxyapatite (FAP). This form of enamel is harder than the naturally occurring HAP and more resistant to decay.
When the outer surface of the enamel is exposed to fluoride (>50ppm), calcium fluoride (CaF2) can be formed.
This can be precipitated on the enamel surface and act as a source of fluoride ions, and may also act as a barrier to demineralization. The CaF2-derived fluoride ions are released in acidic conditions and either diffuse rapidly into the underlying enamel, resulting in formation of FAP and subsequent enamel hardening, or increase fluoride levels in the saliva.

View an animation depicting the formation of a fluoride reservoir
The concentration of fluoride ions and pH of the saliva affect the extent and rate of benefit associated with fluoride by altering the degree of saturation of FAP. [Arends et al., 1984] Even at very low concentrations in saliva, fluoride drives the thermodynamic equilibrium for remineralization by calcium and phosphate from saliva.

View an animation depicting how fluoride prevents demineralization
In addition to improving the balance between remineralization and demineralization, fluoride may also have an antimicrobial effect. [ten Cate, 1999] Fluoride inhibits a step in glycolysis, which slows down plaque metabolism. The rate of bacterial reproduction may be decreased leading to a reduction in rate of plaque growth and acid production.

View an animation depicting the action of demineralization and remineralization in the mouth
In summary, fluoride can protect against caries in three distinct ways.