Each oxygen atom needs to share two electrons to achieve a stable electronic structure of a noble gas.
However, one hydrogen atom can only share one electron to achieve its own stable electronic structure.
Therefore, one oxygen atom will share 2 valence electrons with 2 hydrogen atoms to form the H2O molecule.