In the compound #NaCl#, the sodium and chlorine are held together by what?
An ionic bond.
Atoms want to have completely full valence electron shells (or completely empty shells, making the shell underneath completely full and the new outer shell). To do this, they must lose electrons or gain electrons to fill or remove spots from their shells.
Sodium only has 1 valence electron in its outer shell. It is very unstable on its own because of this; it is very close to being stable and wants to gain stability as soon as possible.
Chlorine has 7 valence electrons in its outer shell. It needs 8 (like most other atoms) to fill its shell completely, so it is 1 electron away from being full. Because of this, it is very unstable on its own and wants to gain stability as soon as possible.
To fix this problem, this happens:
Sodium gives up its electron to create an empty shell, making the shell underneath it completely full.
Chlorine takes this electron to create a full shell!
This kills two birds with one stone (the movement of a single electron, in this case), and both molecules are now stable.
However, since Sodium has lost an electron, it now has a positive charge. And, since Chlorine has gained an electron, it now has a negative charge.
Since opposites attract, the positive Sodium sticks to the negative Chlorine to make one formula unit of Sodium Chloride.
This is how ionic bonding works.