Does God Punish to the Fourth Generation?
God forgives repentant sinners, but punishes the individually guilty.
Exodus 20:5-6 (RSV) you shall not bow down to them or serve them; for I the LORD your God am a jealous God, visiting the iniquity of the fathers upon the children to the third and the fourth generation of those who hate me,  but showing steadfast love to thousands of those who love me and keep my commandments.
This passage and its erroneous interpretation are old chestnuts of anti-Christian polemics. But at least it is understandable that it would be a difficulty (at face value), because this is a somewhat complex concept to fully understand.
John W. Haley, in his book, Alleged Discrepancies of the Bible (1874), provides perhaps the best short summary of this theme that I’ve seen:
[W]e may say that Jehovah “visits” the iniquity of the fathers upon their children, in that he permits the latter to suffer in consequence of the sins of the former. . . .
Even if the above text conveys the idea not only of suffering, but also of punishment, yet the language, “unto the third and fourth generation of them that hate me,” indicates children who are sinful like their parents . . . Plainly children are intended to imitate and adopt the sinful habits and practices of their parents; hence, being morally, as well as physically, the representatives and heirs of their parents, they may be, in a certain sense, punished for the sins of those parents.
Bible passages of this sort exaggerate God’s traits in a non-literal way in order to make Him more understandable to man (what is called anthropomorphism by Bible scholars). A similar analogy is the theme of “God hardening hearts” (see, e.g., Ex 7:3; Rom 9:18): which is another way of saying that “God in His providence allowed Person X to harden his own heart” (see, e.g., Ex 9:34; Heb 3:8) All the relevant biblical texts along those lines, when considered as a whole, show this clearly.
In my research on this topic, I found twenty Bible passages referring to man being judged for his own sin, not that of another. For example:
2 Kings 14:6 But he did not put to death the children of the murderers; according to what is written in the book of the law of Moses, where the LORD commanded, “The fathers shall not be put to death for the children, or the children be put to death for the fathers; but every man shall die for his own sin.”
Jeremiah 31:30 But every one shall die for his own sin . . .
Ezekiel 18:20 The soul that sins shall die. The son shall not suffer for the iniquity of the father, nor the father suffer for the iniquity of the son; the righteousness of the righteous shall be upon himself, and the wickedness of the wicked shall be upon himself.
2 Maccabees 7:32 For we are suffering because of our own sins.
1 Peter 1:17 . . . who judges each one impartially according to his deeds . . .
Moreover, by consulting all related passages, we find at least three in which both concepts are present together (inter-generational punishment and individual accountability):
Exodus 34:6-7 The LORD passed before him, and proclaimed, “The LORD, the LORD, a God merciful and gracious, slow to anger, and abounding in steadfast love and faithfulness,  keeping steadfast love for thousands, forgiving iniquity and transgression and sin, but who will by no means clear the guilty, visiting the iniquity of the fathers upon the children and the children’s children, to the third and the fourth generation.”
Numbers 14:18-20 The LORD is slow to anger, and abounding in steadfast love, forgiving iniquity and transgression, but he will by no means clear the guilty, visiting the iniquity of fathers upon children, upon the third and upon the fourth generation.’  Pardon the iniquity of this people, I pray thee, according to the greatness of thy steadfast love, and according as thou hast forgiven this people, from Egypt even until now.”  Then the LORD said, “I have pardoned, according to your word;
Jeremiah 32:17-19 `Ah Lord GOD! . . .  who showest steadfast love to thousands, but dost requite the guilt of fathers to their children after them, O great and mighty God whose name is the LORD of hosts,  great in counsel and mighty in deed; whose eyes are open to all the ways of men, rewarding every man according to his ways and according to the fruit of his doings;
This suggests that such punishment “to the third and fourth generations” applies only to children who deliberately choose to follow the sinful ways of their parents, and is not stated in any absolute sense that would preclude individual pardon.
Thus, the two strains are not ultimately contradictory, once one understands their sense. These three passages provide the interpretive key: God forgives repentant sinners, but punishes the individually guilty. Note that Exodus 34:6 provides a counter-balance of mercy to Exodus 34:7.
If we are to make much of God talking about punishment over three or four generations (setting aside how to interpret that, for a moment), then we ought to also notice two passages that strikingly highlight God’s extraordinary mercy:
Deuteronomy 7:9 Know therefore that the LORD your God is God, the faithful God who keeps covenant and steadfast love with those who love him and keep his commandments, to a thousand generations,
1 Chronicles 16:15 He is mindful of his covenant for ever, of the word that he commanded, for a thousand generations,
So the “good stuff” and the mercy are described as lasting for a thousand generations, and the “bad stuff” for only four (and even that is adequately explained as non-problematic by the above considerations, in my opinion). That’s 250 times longer for the good things, compared to the bad. The merciful motif is exponentially more prominent (even in the Old Testament) than the judgmental / wrathful God motif.