Sodium is in the most reactive family of metals, the alkali metals (Group 1) and chlorine is in the most reactive family of non-metals, the halogens (Group 17). Sodium, like all alkali metals, has one valence electron. This makes it chemically unstable. Elements are most stable when they have a complete octet of 8 valence (outer) electrons. Sodium readily loses the one electron in its outer level, giving it the stable electron configuration of neon which has 8 valance electrons.
Chlorine, like the other halogens, has 7 valence electrons. It very easily gains one electron to give it a complete octet and the stable electron configuration of argon.
In order for an element to lose electrons it must react with something that gains electrons. Sodium and chlorine react very vigorously because of sodium's high tendency to lose electrons and chlorine's high tendency to gain electrons. This results in the formation of Na+ ions and Cl- ions, which are attracted to each other because of their opposite charges, forming the ionic compound NaCl (sodium chloride):
`2Na + Cl_2 -> 2NaCl`