Electrons love having full valence shells. For some elements like sodium, that means they need to lose an electron. For other elements, like chlorine, that means they need to gain an electron.
They make a perfect match. When sodium gives an extra electron to chlorine, chlorine becomes negatively charged and sodium becomes positively charged. And opposites attract, so the atoms stick together!
In class today, students practiced pulling random elements out of a box, drawing their dot diagrams, and figuring out whether they would form an ionic bond. For most pairs of elements, it’s just not possible. But for some special combinations, like table salt (NaCl) and sodium fluoride (NaF), it just makes sense.