Elements tend to combine with one another to attain the stable electronic configuration of the nearest inert gas.
Sodium chloride Sodium atom has 1 electron in the valence shell which it donates to the chlorine atom with 7 electrons in the valence shell to attain the stable electronic configuration of the nearest inert gas, i.e. 8 electrons in the valence shell. This is known as the octet rule. These elements combine to form sodium chloride.
Hydrogen Hydrogen atom has one electron in the valence shell which it shares with another hydrogen atom having one electron to complete its duplet state, i.e. two electrons in the valence shell and resulting in the formation of hydrogen.