The goal of this paper is to establish generic regularity of free boundaries for the obstacle problem in $mathbb R^n$. By classical results of Caffarelli, the free boundary is $C^infty$ outside a set of singular points. Explicit examples show that the singular set could be in general $(n-1)$-dimensional ---that is, as large as the regular set. Our main result establishes that, generically, the singular set has zero $mathcal H^{n-4}$ measure (in particular, it has codimension 3 inside the free boundary). In particular, for $nleq4$, the free boundary is generically a $C^infty$ manifold. This solves a conjecture of Schaeffer (dating back to 1974) on the generic regularity of free boundaries in dimensions $nleq4$.