Molecules are groups of atoms bonded together in order to form a substance. Each atom has electrons in a cloud surrounding its center. When the atoms hydrogen and oxygen share electrons with one another, they become bonded together.
Oxygen atoms have 6 electrons but become more stable if they have 8 electrons. Hydrogen atoms have 1 electron but become more stable if they have 2 electrons.
Oxygen can share its electrons with two hydrogen atoms in order to become stable and form a water molecule.