The sentence "Parla con LA guardia" doesn't change to "il guardio" even though it might be a male guard. But the case "mio nipote /mia nipota" will change the gender according to the situation. So I am a little confused as to when does the noun change according to gender.
Quite a few things here:
- Guadia is not an Italian word, you probably meant guardia.
- Welcome to Italian, the language of irregularities: Guardia is an irregular noun, it has no male variant. Profession names are divided into two kinds: Simple variance "Il cuoco/La cuoca" (The cook) and variance with the enclitic particle -essa "Il dottore\La dottoressa", "Il professore\La professoressa".
- Nipote is a terrible example, and a wrong one, since, once again, it's an irregular noun. The male and female variants use the same word "nipote": "Il nipote/la nipote", "Mio nipote/mia nipote", this, as you said, is not usually the case: "Il figlio/la figlia", "il gatto/la gatta": (The son\the daughter, the cat)
- Why does this happen? It usually has different reasons, due to how words were written in the original language (usually Latin) and how they came to be translated into Italian. Since they are irregulars, there is no rule about it and you have to learn all the different cases.
- If you want to be confused further: Note that this is a different scenario than collective names like "La flotta" (The fleet), "Lo stormo" (The bird pack) etc. But "La guardia" may, like in English, both be a singular noun or a collective one (The guard as a single individual vs The guard as the collective group of the guards).
If I understand correctly the question, you are asking why linguistic gender does not strictly follow actual gender.
Genders in Italian do not strictly follow real gender. Some nouns related to people are always feminine no matter the gender of the described person, some others are always masculine. There is no strict rule, but a dictionary always helps.
"La guardia" is always feminine even though the guarding person may be a man. On the other hand, "il ministro" is always masculine even if it describes female ministers -- although "la ministra" is sometimes used in an informal, ironic or jocular context.