In Italian, grammatical gender, when referring to people and animals, is related to sex. However, this principle is not always observed. Three distinct examples include: la guardia (guard—usually a man), il soprano (a woman), l'aquila (eagle—male or female).
Regarding things, the attribution of gender may seem unrelated with respect to meaning. For example, there is no logical reason for which il latte (milk) and il sale (salt) "should" be masculine (notably, in Venetian dialect both are feminine). To the contemporary Italian speaker the choice between masculine or feminine seems to be either totally arbitrary, or, in the case of derivative nouns, simply a matter of grammatical fact (e.g., nouns ending with the suffix -zione are feminine, while nouns ending with the suffix -mento are masculine).
For today's speaker, a historical explanation does not count; the contemporary perspective must remain distinct from the diachronic (which concerns the evolution of language). Italian nouns, for the most part, retain their gender from the Latin. Nouns originally neutral in Latin usually became masculine. There have been some changes, though: from the Latin word folia, the neuter plural of folium, in Italian became foglia (leaf), feminine singular (because in Italian the ending -a, in the majority of cases, is feminine and singular). The conformity to this rule is also illustrated in the assignment of gender to foreign words used in Italian.
That the assignment of gender is immaterial with respect to the inherent meaning of things is born out by a comparison between diverse languages, even though they are related to one another: Italian, French, and Spanish.
Consider the following:
Masculine in Italian / Feminine in French:
il dente—la dent (tooth), il costume—la coutume (costume), il fiore—la fleur (flower), il mare—la mer (sea)
Feminine in Italian / Masculine in French:
la coppia—le couple (couple), la mescolanza—le mélange (mixture), la sciabola—le sabre (saber)
Masculine in Italian / Feminine in Spanish:
il costume—la costumbre (costume), il fiore—la flor (flower), il latte—la leche (milk), il miele—la miel (honey), il sale—la sal (salt), il sangue—la sangre (blood)
Feminine in Italian / Masculine in Spanish:
la cometa—el cometa (comet), la domenica—el domingo (Sunday), l'origine—el origen (origin)
English is much easier, since grammatical gender is not recognized except in rare cases. Conversely, German, much like Latin, also has the neuter gender. There are significant differences between the Italian and German with regard to gender; for instance il sole (the Sun) is feminine (die Sonne), while la luna (the Moon) is masculine (der Mond).