Assigning nouns to a certain noun class, with other words taking various forms by agreeing with that noun class (e.g. adjectives, determiners, or verbs marking the noun's gender) allows you to spread some of the information about what that noun is around the sentence, increasing redundancy.
Contrary to many naïve assumptions, this is actually a good thing. It makes it easier to reconstruct the utterance if part of it is lost (e.g. a sudden noise means you fail to hear one particular syllable).
Case in point, in Spanish someone describing the rules of a game might refer to un polo "a pole" and una bola "a ball", two words that might be easily misheard for each other. Luckily, in this case even if I don't hear the initial consonant clearly, the gender on the article allows me to distinguish between the two. If the item in question is white e.g. un polo blanco "a white pole" or una bola blanca "a white ball", I can do this even if I also fail to hear the article clearly.
Ultimately the same pressure (redundancy being good) is behind all agreement phenomena (including things like subject person & number on verbs).
Before this I have mostly spoken according to received wisdom as I have understood it (unfortunately I don't remember any particular sources to cite for it though), whilst the following is my own deduction from that and some knowledge of the mathematics of information theory.
When it comes to assigning nouns to specific classes there are two competing pressures. One is wanting the number of nouns in each class (weighted by frequency of that noun) to obey a specific maximum-entropy distribution, and thus maximise the likelihood of knowing the class of noun being able to help you identify the noun. The other is for the noun classes to be reasonably "natural" and so making it easy to recall the class of a noun provided you know its meaning.
The tendency seems to be for languages with many noun classes (e.g. Bantu) to lean into using natural classes at the expense of the distribution, whilst those with few noun classes (e.g. Indo-European languages) to lean into having an efficient distribution at the expense of naturalness, with most nouns seemingly assigned randomly with personal names, words for livestock, and the words for "husband" and "wife" being some of the few words that can reasonably reliably be assumed to have their natural gender (although even there, note that both "wife" was originally neuter in English, and "woman", being equivalent to wife-man was originally masculine).