Noun classification is a tricky thing. It's so complicated that linguists do not have a full and satisfying explanation for how and why it exists but it's so important to many languages that even starting language learners cannot avoid it.
Proto-Indo-European had two genders, animate and inanimate, that later split the animate class into masculine and feminine while the inanimate remained more-or-less intact as the neuter. Only the oldest Indo-European languages preserved the animate/inanimate distinction (e.g., Hittite and Luwian), while later IE tongues preserve the masculine/feminine/neuter (e.g., Sanskrit, Greek, German) or have even more changes (e.g., English, French, Sorani).
Other language families capture different types of noun classifications. At no point in history, to our knowledge, did Basque have grammatical gender. Depending on the language in question, Niger-Congo languages almost always have at least 10 classes.
There are many ways noun classification is governed, but the key trait is that there is a semantic distinction between classes. It's not always cut and dry. Russian has gender in the singular but plural nouns are genderless. Aikhenvald (2000) divides the government of noun classification as follows:
- Pure semantic assignment. Here, classification of the noun is understood on the basis of its meaning.
- Tamil divides gender between rational and non-rational nouns. In the former belong humans, gods, and demons.
- Dyirbal has four complicated classes: group one covers males humans and non-male animates; group two is female humans, water, fire, and fighting; group three covers non-flesh food; and group four is simply everything else.
- Phonological assignment. There is no language that solely relies upon the sound structure of a word to assign a noun class. Some languages do seem to have a tendency to classify a noun on the basis of what sounds are present in a word, even if the classification does not semantically fit.
- Limilngan typically classifies nouns on a semantic basis (like the languages above). However, Limilngan can form "super-classes." If a new word has /l/ or /d/, it is assigned to Class II (animals), while words with /m/ are assigned to Class III (plants), even if the words have nothing to do with animals or plants.
- Impure semantic assignment. Some languages have classification principles that fall outside the realm of definitions, though as we discussed before, no known language classifies entirely independent of a noun's semantic value. These reasons can be suprasegmental or morphological.
- In the Harar dialect of Oromo, animate masculine and feminine nouns correspond to their biological sex, but inanimate nouns are classified as masculine or feminine on the basis of whether the word ends in a low vowel (masculine) or not (feminine).
- Latin may decline a word in one gender but it will grammatically agree according to its biological sex (e.g., the office of an agricola "farmer" belonged to a man in ancient times; the word is first declension and therefore declines as feminine but is grammatically treated as masculine).
- Iraqw nouns that derive from Class I verbs are treated as masculine and nouns from Class II verbs are feminine, irrespective of semantics or phonology.
- Ket distinguishes masculine and feminine corresponding to biological sex. Except that important objects like wood are also masculine; the sun (masculine) and moon (feminine) is due to its role in Ket mythology; everything else is neuter.
- Unknown motivation. Sometimes it is not understood why nouns are classified the way they are. Hebrew divides nouns along masculine and feminine lines according to their semantic meaning or its morphology. But a small collection of words like eš "fire" belong to the feminine gender even though the masculine is expected. There is no explanation for this.
If you want to learn more about world languages and how we, as humans, divide the world into categories through our language, check out Aikhenvald's fantastic book:
Aikhenvald, Alexandra. Classifiers: A Typology of Noun Categorization Devices. Oxford University Press. 2000.