Electrons are shared in a covalent bond when each of the participating atoms has roughly the same ability to attract electrons. The more evenly the two atoms are able to pull the participating electrons towards themselves, the more evenly the electrons share their time around each atom.
Covalent bonds are often formed between two identical elements because each element has the same ability to attract covalent electrons. This ability to attract electrons is called electronegativity, and has a numerically assigned energy value, usually in electron volts. The closer two covalently bonded atoms are in electronegativity, the more covalent the bond is. With the exception of identical atoms, all other covalently bonding atoms have different electronegativities, and thus cannot form purely covalent bonds.
As the difference in electronegativity increases, the electrons spend more time around the more electronegative atom, imparting it with a partial negative charge, while the less electronegative element gains a partially positive charge. When the difference in electronegativity is sufficient to allow the more electronegative element to take the electrons of the less electronegative one, an ionic bond is formed. A covalent fraction can be calculated for two dissimilar elements, indicating how covalent or ionic their bond would be.