Some atoms are more stable when they gain or lose an electron (or possibly two) and form ions. This results in a full outermost electron shell and makes them energetically more stable. Now, because the number of electrons does not equal the number of protons, each ion has a net charge.