This is because carbon has four valence electrons (electrons in its outermost shell), and it needs to gain or lose four electrons to achieve a stable octet configuration. It can achieve this by sharing its electrons with other atoms to form covalent bonds.