All atoms are made up of a positively charged nucleus surrounded by negatively charged electrons. The outermost electrons -- the valence electrons -- are able to interact with other atoms, and, depending on how those electrons interact with other the atoms, either an ionic or covalent bond is formed, and the atoms fuse together to form a molecule.
Every element is surrounded by a certain number of electrons that populate electron orbitals. Each orbitals requires two electrons to be stable, and the orbitals are organized into shells, with each successive shell being of a higher energy level than the previous one. The lowest shell contains only one electron orbital, 1S, and, thus, requires only two electrons to be stable. The second shell (and all those that follow) contains four orbitals -- 2S, 2Px, 2Py and 2Pz (one P for each axis: x, y, z) -- and requires eight electrons to be stable.
Going down the rows of the Periodic Table of the Elements, a new shell of 4 electron orbitals, with the same setup as the second shell, exists around each element. For example, Hydrogen in the first row has only the first shell with one orbital (1S) while Chlorine in the third row has the first shell (1S orbital), the second shell (2S, 2Px, 2Py, 2Pz orbitals) and a third shell (3S, 3Px, 3Py, 3Px orbitals).
Note: The number in front of the each S and P orbital is an indication of the shell in which that orbital resides, not of quantity.
The electrons in any given element's outer shell are its valence electrons. Since all elements want to have a full outer shell (eight electrons), these are the electrons that it is willing to either share with other elements to form molecules or to give up entirely to become an ion. When elements share electrons, a strong covalent bond is formed. When an element gives away an outer electron, it results in oppositely charged ions that are held together by a weaker ionic bond.
All elements start with a balanced charge. That is, the number of positively charged protons equals the number of negatively charged electrons, resulting in an overall neutral charge. However, sometimes an element with only one electron in an electron shell will give up that electron to another element that needs only one electron to complete a shell.
When that happens, the original element drops down to a full shell and the second electron completes its upper shell; both elements are now stable. However, because the number of electrons and protons in each element are no longer equal, the element that received the electron now has a net negative charge and the element that gave up the electron has a net positive charge. The opposing charges cause an electrostatic attraction that pulls the ions together tightly into a crystal formation. This is called an ionic bond.
An example of this is when a sodium atom gives up its only 3S electron to fill the last shell of a chlorine atom, which needs only one more electron to become stable. This creates the ions Na- and Cl+, which bond together to form NaCl, or common table salt.
Instead of giving away or receiving electrons, two (or more) atoms may also share electron pairs to fill their outer shells.This forms a covalent bond, and the atoms are fused together into a molecule.
An example of this is when two oxygen atoms (six valence electrons) encounter carbon (four valence electrons). Because each atom wants to have eight electrons in its outer shell, the carbon atom shares two of its valence electrons with each oxygen atom, completing their shells, while each oxygen atom shares two electrons with the carbon atom to complete its shell. The resulting molecule is carbon dioxide, or CO2.