Look at the messy Bohr structures in Figure 4, and you’ll see that the two chlorine atoms in a molecule of chlorine gas are sharing an electron. The chlorine atoms are cheating, so to speak, in their attempt to achieve a stable configuration of eight electrons in the outermost shell. By sharing an electron, the two chlorine atoms manage to (sort of) fill out their outer shells and achieve a low-energy, stable configuration.

The same goes for hydrogen, although like helium it doesn’t exactly follow the octet rule. That’s because hydrogen and helium have only one shell, and that shell achieves a noble gas configuration, an "octet" as it were, at two electrons. But hydrogen has only one electron in that single shell. So when two hydrogen atoms meet, they share their electrons to complete their outer shell and achieve a stable configuration like the noble gas helium.