This tendency to gain electrons is due to their high electronegativity, which is the ability of an atom to attract electrons. The more electronegative an atom is, the stronger its pull on electrons and the more likely it is to gain electrons in a reaction.
When a nonmetal gains electrons, it acquires a negative charge and is said to be reduced. The oxidation number of a nonmetal decreases as it gains electrons, becoming more negative.
For example, in the reaction between sodium (Na) and chlorine (Cl) to form sodium chloride (NaCl), chlorine gains one electron from sodium, becoming a negatively charged chloride ion (Cl-). The oxidation number of chlorine changes from 0 in elemental chlorine to -1 in the chloride ion.
In summary, nonmetals tend to have negative oxidation numbers because they gain electrons in chemical reactions, becoming negatively charged ions. This is due to their high electronegativity and the tendency to undergo reduction.