Unlike in English, nouns in Spanish accept the grammatical property of gender, a concept that may confuse native English writers. I have searched for a reliable source that explains why nouns have gender in Spanish and have not found one yet, but I gather gender was used not only to reflect the sexuality of humans and animals but also to clarify. But this philosophical discussion strays us from the concept of gender, which must be absorbed first to then understand the rules of nominal agreement. Regarding the ideas about sexuality and clarity, consider the following example.
[The female cat [sleeps on the rug].
[La gata] [duerme en el tapete].
What is named in English using three words is named in Spanish using only two words. It is true that some nouns in English only refer to the feminine sex, but in Spanish, all nouns express gender, which is not the same as sex. The concept of gender pertains only to words while that of sex considers the sexuality of the being it refers to. All nouns and some pronouns are classified by gender as either masculine or feminine; nouns designating sexed beings (persons, animals, and some plants) are further categorized into four classes.
According to El buen uso del español, some nouns in Spanish “express the differences in gender and, consequently, of sex by means of morphemes (terminations) added to the word of the same root” (136). This first class of nouns, such as gato/gata or barón/baronesa, is called nouns of variable termination. You can determine if nouns are part of this class by typing the masculine (also called the neutral) noun on the RAE website. If the entry provides two terminations, it is a variable termination noun.
The second class of nouns is referred to as common nouns regarding gender and includes the names of people that designate women as well as men; these nouns are spelled the same regardless of their gender and are only differentiated with either a masculine or feminine determiner or adjective. These nouns are categorized depending on the termination: el astronauta/la astronauta, el cónyuge/la cónyuge, el maniquí/la maniquí, el modelo/la modelo, el canciller/la canciller, etc. (138-139). But some nouns are the exceptions of this class—I imagine their usage has made them irregular—and their termination varies depending on the sex of the person they refer to. Verify whether nouns change termination on the RAE website.
The third class of nouns “express the masculine/feminine grammatical difference and, simultaneously, the opposition of sex ‘man’/ ‘woman’ for people or ‘male’/ ‘female’ for animals through terms with a different root,” such as hombre (man) and mujer (woman) or caballo (horse) and yegua (mare) (136); in Spanish, these are called heterónimos, which does not translate to heteronym because this English word does not consider gender in its definition.
Epicene nouns, the fourth class, “are the names of only one gender that designate animate beings without specifying their sex”. This is to say that the agreement of epicene nouns with other words is based on the gender of the noun rather than on the sex of the being they refer to. For example, when referring to animals, determine the sex of the epicene noun using either the word macho (male) or hembra (female): tiburón macho or tiburón hembra. The adjectives or pronouns that refer to tiburón must all be male even when the shark being described is female. When referring to people, specify the gender using the word masculino (masculine) or femenino (feminine): persona masculina or persona femenina (148-149). You can determine whether the noun is epicene or not by searching for it on the RAE website. The result will indicate that the noun is only used in its male form.
Regarding the gender of objects and asexual beings, nouns are either masculine or feminine without reflecting sex; I imagine objects obtained the grammatical property of gender because of philosophical debates dating back to when Latin, one of Spanish’s predecessors, was being developed. But as a general rule, masculine gender nouns that refer to objects or asexual beings tend to end with the letter o and feminine gender nouns tend to end with the letter a. Nouns ending in different letters can be either masculine or feminine, so it’s better to search for the term on the RAE website to find out its gender.
Some nouns are denominated ambiguous regarding gender because they can either be used as masculine or feminine to refer to one same thing. I use the word thing because most of these nouns refer to inanimate beings: an example is el mar – la mar (150-151). Although different factors may explain this condition, I believe geography and culture created this ambiguity.
Additionally, in Spanish, the masculine gender is used as the neutral gender when referring to “the entire class, that is, to all the individuals of the species, without distinguishing sexes”. The following example indicates that dogs and bitches are man’s best friend.
[El perro] [es el mejor amigo del hombre.]
Therefore, the plural masculine gender is used to refer to the names applied to people, animals, or things of both genders.
[Los empleados] [reclamaron un aumento de sueldo.] 
The pursuit of sexist-free language, like in English, is something unrealistic in Spanish because of its intrinsic grammatical property of gender. El País, one of the most read newspapers in Spain, wrote an article about this issue in 2012.