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 in each electronic state of a molecule, there exists 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 exists 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 the barrier be between two minima so that we say they are well separated? All this depends on the problem at hand and the corresponding physical conditions.
For instance, if we are talking about molecules in a gas phase under usual temperatures ($300 \ \text{K}$), then, say, ethane is a non-rigid molecule since its conformations are separated by just a few $\text{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 $\mathrm{CO_2}$ and $\mathrm{H_2O}$, 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 the arrangement of nuclei. We should be careful since, in general (as we have already said), we are not guaranteed that there exists only one minimum, but for small molecules such as $\mathrm{CO_2}$ and $\mathrm{H_2O}$, that is not an issue.
So, if you do these calculations, you will indeed find out that (at least in their ground electronic states) $\mathrm{CO_2}$ and $\mathrm{H_2O}$ 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 temperature, nuclei are constantly vibrating near the equilibrium. So if you perform some physical measurement (say, GED), you won't necessarily "catch" each and every molecule in the equilibrium geometry; rather, you obtain the average picture.