Non-metals are found on the right side of the periodic chart. Their valence shell of electrons is either full (noble gases) or nearly full. They can fill their valence shells easier by gaining electron than losing them. But if they can't gain electrons, sharing is better than losing.
Example: Oxygen has six valence electrons. It would rather gain two electrons than lose six. But if it can't gain two, it would be easier to share two electrons with another atom that would share two of its electrons also. This would result in the formation of two covalent bonds. This would be easier than losing six electrons.