Here's how it happens:
* Sodium is a highly reactive metal that wants to lose one electron to achieve a stable electron configuration.
* Chlorine is a highly reactive nonmetal that wants to gain one electron to achieve a stable electron configuration.
When sodium and chlorine react, sodium loses an electron, forming a positively charged sodium ion (Na+). Chlorine gains that electron, forming a negatively charged chloride ion (Cl-). These oppositely charged ions are attracted to each other and form an ionic bond, creating the compound sodium chloride.