Your understanding is correct to some extent. Tooth enamel mostly consists of a mineral called hydroxyapatite ($\ce{Ca_{10}(PO4)6(OH)2}$) (Staines et al, 1981). Though this makes enamel the strongest material in the body, it is susceptible to degradation. In acidic environment, hyrdoxyapatite gets dissolved and leads to cavities (Brown, p. 688). The reaction is:
$\ce{Ca_{10}(PO4)6(OH)2_{(s)}~+~8~H+_{(aq)} -> 10~Ca^{2+}_{(aq)}~+~6~HPO4^{2-}_{(aq)}~+~2~H2O_{(l)}}$
To overcome this, fluoride is added in toothpastes. Fluoride ($\ce{F-}$) reacts with hydroxyapatite of enamel to form fluoroapatite via demineralization and remineralization cycle. The 3 main mechanisms through which it happens are (Rošin-Grget et al, 2013):
Iso-ionic exchange of $\ce{F-}$ for $\ce{OH-}$ in apatite:
$\ce{Ca_{10}(PO4)6(OH)2~+~2~F- -> Ca_{10}(PO4)6F2~+~2~OH-}$
Crystal growth of fluorapatite from a supersaturated solution:
$\ce{10~Ca^{2+ }~+~6~PO4^{3-}~+~2~F- -> Ca_{10}(PO4)6F2}$
Apatite dissolution with $\ce{CaF2}$ formation:
$\ce{Ca_{10}(PO4)6(OH)2~+~20~F- -> 10~CaF2~+~6~PO4^{3-}~+~2~OH-}$
Instead of preventing cavities, fluoride just controls the rate of formation of cavities (i.e. it repairs, not prevents, tooth decay), due to which its repeated exposure on enamel throughout the day is important (Cate et al, 2013). However, the effects of fluoride are minimal once it gets swallowed (Ismail et al, 2008).
Talking about fluorosis, the cause behind it is dissolution of hydroxyapatite due to higher concentration of fluoride (see the 3rd point in mechanisms, it requires higher level of fluoride to occur). Apart from this, formation of fluorohydroxyapatite is also a cause of fluorosis, the reaction being:
$\ce{Ca_{10}(PO4)6(OH)2~+~F- ~+~H+ -> Ca_{10}(PO4)6(OH)F + H2O}$
Dissolution of fluoroapatite causes depletion of enamel layer, causing fluorosis.
References:
M. Staines, W. H. Robinson and J. A. A. Hood (1981). "Spherical indentation of tooth enamel". Journal of Materials Science. 16 (9): 2551–2556. doi:10.1007/bf01113595.
Wikipedia contributors. "Tooth enamel." Wikipedia, The Free Encyclopedia. Wikipedia, The Free Encyclopedia, 25 Apr. 2017. Web. 1 May. 2017.
Rošin-Grget, K; Peroš, K; Sutej, I; Bašić, K (Nov 2013). "The cariostatic mechanisms of fluoride". Acta medica academica. 42 (2): 179–88. doi:10.5644/ama2006-124.85. PMID 24308397. Retrieved 31 March 2014.
ten Cate FM. Contemporary perspective on the use of fluoride products in caries prevention British Dental Journal 214, 161 - 167 (2013) PMID 23429124
Ismail AI, Hasson H; Hasson (2008). "Fluoride supplements, dental caries and fluorosis: a systematic review". J Am Dent Assoc. 139 (11): 1457–68. doi:10.14219/jada.archive.2008.0071.