Each element tries to achieve stability by forming a noble gas configuration (2 or 8 valence electrons)
Carbon has 4 valence electrons, achieving stability requires 4 more electrons
Hydrogen has 1 valence electron, achieving stability requires 1 more electron
Carbon and hydrogen bond covalently by sharing their electrons so that they become stable (carbon binds 4 hydrogens) to form CH₄ (methane)