The electrons that are more loosely held by the nucleus (the valence electrons, those furthest away from the nucleus) are the ones that are gained, lost, or shared.
Electrons are negatively charged, while the nucleus has a positive charge because of the protons. The protons attract and hold the electrons, but the farther away the electrons are, the weaker the attractive force.Chemists really only consider the electrons in the s and p orbitals in the energy level that is currently being filled as valence electrons.
In the electron configuration for oxygen (shown below), energy level 1 is filled, and there are 2 electrons in the 2s orbital and 4 electrons in the 2p orbital for a total of 6 valence electrons. Those valence electrons are the ones lost, gained, or shared.
Being able to determine the number of valence electrons in a particular atom gives you a big clue as to how that atom will react.