Here are the places where that phrase is used in the Bible:
Exodus 34:7 Keeping mercy for thousands, forgiving iniquity and transgression and sin, and that will by no means clear the guilty; visiting the iniquity of the fathers upon the children, and upon the children's children, unto the third and to the fourth generation.
Numbers 14:18 The LORD is longsuffering, and of great mercy, forgiving iniquity and transgression, and by no means clearing the guilty, visiting the iniquity of the fathers upon the children unto the third and fourth generation.
Exodus 20:5 Thou shalt not bow down thyself to them, nor serve them: for I the LORD thy God am a jealous God, visiting the iniquity of the fathers upon the children unto the third and fourth generation of them that hate me;
Deuteronomy 5:9 Thou shalt not bow down thyself unto them, nor serve them: for I the LORD thy God am a jealous God, visiting the iniquity of the fathers upon the children unto the third and fourth generation of them that hate me,
Note that if we were to read Exodus 34:7 and Numbers 14:18 as standalone comments, it would seem that God is being grossly unfair by punishing the children for the sins of their ancestors. In fact, God says specifically in Ezekiel chapter 18 that He would not do such a thing. That's why we have other passages with teaching on the same subject. We must take the Bible as a whole and not just isolated passages. So, in Exodus 20:5 and Deuteronomy 5:9 we have the key additional phrase: "of them that hate me".
The children of a sinful generation or society who continue in the sins of their fathers will receive their just punishment. However, any member of a sinful family or society who turns from their wickedness will be forgiven. Think of Rahab the Canaanite in the book of Joshua. Her whole society, because of their vile idolatry, was devoted by God to utter destruction. However, Rahab and her family forsook their people to bind themselves to the people of God and they were welcomed and saved from the destruction visited upon their people by Joshua when Jericho was destroyed. In fact, Rahab became an ancestor of the Lord Jesus Christ!
I hope you have found this helpful.