Any chemical reaction involves an energy change or transfer. Atoms combine to form molecules, because the sharing or transferring of electrons result in a lower, and more stable energy state. That is, energy input from endothermic reactions are stored in the bonds; exothemic reactions release energy as heat and radiation; oxidation-reduciton reactions involves the interconversion of chemical and electrical energy to do work, etc.
In a covalent bond, two nonmetals share electrons equally to fullfill their valence electron configurations. Hydrogen's 1s1 can hold one more electron, and oxygen's (1s22s22p4) 2p orbitals can hold two more electrons. Oxygen, however, is more electronegative than hydrogen; it pulls the shared electrons closer to itself, and away from the hydrogen. The result of the unequal sharing of electrons is a polar covalent bond, and a molecule that is dipolar, or a molecule that has a dipole momment. Due to the greater electron distribution around oxygen, it is the center of a partial negative charge. Hydrogen is the center of the partial positive charge.
In ionic bonding, a metal and nonmetal combine by a transfer of electrons to complete their outer shell. Generally, the more electrognegative nonmetals tend to gain electrons to form anions; whereas, metals tend to loose electrons easily, and become cations. There is no totally ionic character however, in any bond types. A transfer of electron is considered when the difference in electrogenativities between the bonded atoms are significantly large.
Furthermore, the polarity of the molecule is determined by its geometry, and the vector sum of the diploes. For example, CH3Cl (methyl chloride) is polar, but CCl4 is nonpolar. Thus, linear, trigonal planar, tetrahedral, etc., molecules with identical groups have dipoles which cancel, and are nonpolar. The geometry is determined by either the VSEPR model, or Molecular Orbital Model.