The reason why some atoms gain or lose electrons to form ions is because a full outermost electron shell and makes them energetically more stable,and because of the number of the electrons does not equal to the number of protons is because each ion has a net charge, hope this helps :)