One formula unit of sodium chloride contains an ionic bond. The sodium loses an electron to become positively charged, the chlorine atom accepts the electron to become negatively charged, and the two oppositely charged ions attract each other. This electrostatic force of attraction is called an ionic bond.
All atoms form chemical bonds that allow them to obtain an electron configuration that is identical to the nearest noble gas. Noble gases are chemically inert and stable. Atoms of other elements achieve this chemical stability by losing, gaining or sharing electrons with other atoms. Atoms that lose or gain electrons to achieve the noble gas configuration form ionic bonds, while atoms that share electrons form covalent bonds.
Sodium has 11 electrons. Its nearest noble gas, neon, has 10 electrons. Sodium can lose the electron in its outermost shell to get an electron configuration identical to neon, thus making it chemically stable. However, when sodium loses an electron, it becomes positively charged. The sodium ion can become electrically neutral again while maintaining chemical stability by bonding to a negatively charged atom.
Chlorine has 17 electrons, which is one electron short of its nearest noble gas. Chlorine can gain an electron, thus gaining a negative charge, to achieve chemical stability. In order to remain electrically neutral, the chloride ion can bind to a positively charged sodium ion and form an ionic bond. Sodium chloride is an electrically neutral compound which is also chemically more stable than its individual components.