Rashi addresses your question: God visits the iniquity of parents on their children only when the children follow in their parents' sinful and rebellious ways. People tend to learn values, good and bad, from their parents, after all -- but sin is not a condition that sticks to you merely through inheritance like some other religions claim. Once a person realizes he is doing wrong and repents of it, God does not continue to punish him for it, because the other place where this is brought up (in the decalogue), it says that God punishes "those who hate Me". People who have repented of their transgressions don't fall into this category.
Rashi on Shemot 34:7 (emphasis mine):
[iniquity and rebellion]: Iniquities (עִוֹנוֹת) are intentional sins. פְּשָׁעִים are sins committed out of rebellion, which a person commits [in order] to anger [God]. -[from Yoma 36b]
yet He does not completely clear [of sin]: Heb. וְנַקֵּה לֹא יְנַקֶּה. According to its simple interpretation, it means that He does not completely overlook the iniquity but exacts retribution for it little by little. Our Rabbis, however, interpreted [this expression to mean]: He clears those who repent, but does not clear those who do not repent (from Yoma 86a, targumim).
He visits the iniquity of parents on the children: when they hold onto the deeds of their parents in their hands [i.e., emulate their ways], for He already explained this in another verse, [that it means only] “of those who hate Me” (Exod. 20:5). -[from Ber. 7a]
See the sources Rashi cites for more information.
Shemot 20:5 says (emphasis mine):
You shall neither prostrate yourself before them nor worship them, for I, the Lord, your God, am a zealous God, Who visits the iniquity of the fathers upon the sons, upon the third and the fourth generation of those who hate Me
Rashi explains:
of those who hate Me: As the Targum [Onkelos paraphrases: when the sons continue to sin following their fathers, i.e.], when they cling to their fathers’ deeds. — [from Sanh. 27b]