Recent works have shown that the domain walls of room-temperature multiferroic BiFeO3 (BFO) thin films can display distinct and promising functionalities. It is thus important to understand the mechanisms underlying domain formation in these films. High-resolution x-ray diffraction and piezoforce microscopy, combined with first-principles simulations, have allowed us to characterize both the atomic and domain structure of BFO films grown under compressive strain on (001)-SrTiO3, as a function of thickness. The clamping of the substrate has been observed to exist in two different regimes: ultrathin, d < 18 nm, and thin, d > 18 nm. When this is taken into account in the calculations, an excellent agreement between the predicted and observed lattice parameters is shown. We derive a twinning model that describes the experimental observations and could explain why the 71 degrees domain walls are the only ones showing insulating character. This understanding of the exact mechanism for domain formation provides us with a new degree of freedom to control the structure and, thus, the properties of BiFeO3 thin films.