Disclaimer: I will try my best to make this rather long story short.
First of all, we must say that we are talking about the so-called equilibrium geometry - molecular geometry that corresponds to the true minimum on the potential energy surface, a surface which describes the energy of a molecule as a function of nuclear coordinates.
Secondly, potential energy surface (PES) is a mathematical abstraction that appears only in approximate treatments of molecular systems within Born–Oppenheimer approximation. In this approximation the state (or, speaking classically, the motion) of electrons is treated independently from that of nuclei, and each electronic state of a molecule there exist the corresponding PES. This approximation breaks out when two PESes come close to each other or even intersect, but it is generally accurate at least for molecules in their ground electronic state (i.e. electronic state of the lowest energy).
Note, however, that even when PES for a particular electronic state of interest is well separated from PESes corresponding to other electronic states, there might exist more than one minimum on the same PES.
Now, clearly, to meaningfully speak about equilibrium geometry for a molecule in a particular electronic state it is required that different minima on the corresponding PES are well separated, or, in other words, that there exist one distinct minimum on the potential energy surface.
And this is again not always the case: there exist some non-rigid molecules, for which minima are not well separated, or, in other words, which have few equilibrium geometries. And some relatively small external influences can significantly change the molecular geometry.
But how small is small? And how big should be the barrier between two minima so that we say they are well separated? All this depends on the problem at hands and the corresponding physical conditions.
For instance, if we are talking about molecules in a gas phase under usual temperatures (300 K), then, say, ethane is non-rigid molecule, since its conformations are separated by just a few kJ/mol (comparable to energies of thermal motion), and thus, the molecular geometry of ethane constantly changes.
But, aside from this, for rigid (under usual conditions) molecules, like CO2 and H2O, we can meaningfully speak about their equilibrium geometries. And we can calculate equilibrium geometries for, say, ground electronic states, in the Born-Oppenheimer approximation. We can do what is known as the geometry optimization: starting from some initial geometry we try to minimise its electronic energy (including nuclear-nuclear repulsion energy) by varying arrangement of nuclei. We should be careful, since in general (as we have already said) we are not guaranteed that there exist only one minimum, but for small molecules such as CO2 and H2O that is not an issue.
So, if you do these calculation, you will indeed found out that (at least in their ground electronic states) CO2 and H2O molecules have the equilibrium geometry mentioned in books.
But even for rigid (under specified conditions) molecules, one should remember, that even at the absolute zero of temperature nuclei are constantly vibrating near the equilibrium. So if you perform some physical measurement (say, GED) you want necessarily "catch" each and every molecule in the equilibrium geometry, rather you obtain the average picture.