The ionization energy of sodium is $\pu{5.139eV}$. This is the energy absorbed when a neutral sodium atom is stripped of its outermost electron. The electron affinity of chlorine is $\pu{3.62eV}$. This is the energy released when an electron is added to a neutral chlorine atom to form the chlorine anion.
Now suppose we have a system containing exactly one neutral sodium atom and exactly one neutral chlorine atom infinitely far apart so that the systems potential energy is zero. If we wish to make a $\ce{NaCl}$ molecule from our system, we need to put $\pu{5.139eV}$ of energy into the system to ionize the sodium atom. Afterwards, we place the electron we have just pulled off the sodium atom onto the chlorine atom upon which we get back only $\pu{3.62eV}$ of energy. So the total energy of the system has increased by $\Delta E =\pu{5.139eV -3.62eV= 1.519eV}$.
If the total energy of the system has increased after performing the previous supposition, then why does the $\ce{NaCl}$ ionic bond occur? Surely it is less stable than having the two neutral atoms separate from each other? My understanding up till now has been that molecules form when the total energy of the system is lower than the total energy of the sum of its constituents?
Is this all because an individual $\ce{NaCl}$ bond is not stable on its own but it is stable when its within a crystal lattice?
Any help on this issue would be most appreciated!