“jealous.” God is a jealous God, not an envious God. Although in some languages the English words “jealousy” and “envy” are translated from the same word, envy and jealousy are not the same thing. “Envy” is when I don’t have something that someone else has and I want it, so I am envious. In contrast, “jealousy” is when I have something and I am afraid someone else will take it from me, so I am jealous. That is why we speak of a “jealous” husband; he is married to the wife but is afraid another man will lure her away from him. God is a “jealous” God in the sense that He is the God and Lord of people, but other gods and other interests are working to take His people away from Him. Interestingly, the cognate word to the Hebrew word for jealous here in Exodus 20:5 refers to being “red,” and we can picture God becoming hot—red-faced—with emotion when some other god tries to steal His people.
“visiting the iniquity of the fathers on the children.” The Hebrew verb translated “visiting” is paqad (#06485 פָּקַד), and it often describes a divine intervention for blessing or cursing; the coming of good or evil. The NET text note on Genesis 21:1, when God “visited” Sarah, points out that when God “visits” that it “indicates God's special attention to an individual or a matter, always with respect to his people's destiny. He may visit (that is, destroy) the Amalekites [1 Sam. 15:2]; he may visit (that is, deliver) his people in Egypt [cp. Exod. 3:16]. …One's destiny is changed when the LORD ‘visits.’” Here in Exodus 20:5 (cp. Exod. 34:7; Num. 14:18), God “visits” the iniquity of the parents on the children.
The Bible has many examples of people being “visited” for good or for harm. For example, in Genesis 21:1, God visited Sarah and she got pregnant (cp. 1 Sam. 2:21). In Genesis 50:24-25, Joseph said God would visit Israel and bring them out of Egypt. In Exodus 3:16, God said he had visited the Israelites in slavery in Egypt, meaning He had seen their circumstances and had begun the process of delivering them. In Ruth 1:6, God “visited” Israel by ending the famine so there would be food. In Psalm 106:4, the psalmist asks to be “visited” with deliverance.
People can be visited for harm as well as for good. In psalm 59:5, the psalmist asks God to “visit” (punish) the nations, and in Psalm 89:32, God said he would “visit” with a rod because of people’s transgression. Proverbs 19:23 says the person who fears God will not be visited with evil. Isaiah 26:21 speaks of God coming to punish (“visit”) the inhabitants of the earth for their iniquity.
“third and fourth generation.” This seems unreasonable to us today, but that is in part due to the fact that for most of us, our grandparents are over 50 and our great-grandparents are dead or close to death by the time we are born. That was not the case in the biblical world. It was quite common for a woman to have a child by 15, and so be a grandparent at around 30, a great-grandparent at about 45, and a great-great-grandparent around 60. Also, in contrast to today, in the biblical world families generally either lived together or in very close proximity. So if a person truly hated God and was sinful, hateful and devilish, the sin he would commit and the effects of that sin would affect everyone in his family for generations.
Parents can sin in such a way that their houses are afflicted by demons and their children are cursed. Furthermore, although those curses can be broken, they still adversely affect the children while they are in place. Also, it is common that children pick up the habits of the parents and members of the household such that the children end up participating in the evil of the parents and thus bring the consequences of their own sin upon them.
Ezekiel 18:20 says that the sons will not suffer punishment for the sins of the fathers, but that promise does not cover every sin. For one thing, we all know children who have suffered due to their parent’s sin. The context of Ezekiel 18:20 is everlasting life or everlasting death, and it is true that a parent’s sin and rejection of salvation will not keep a child from being saved. In contrast, one reason that parents should avoid sin and ungodliness is that it can harm the children, just as Exodus 20:5 says.