First, it's important to clarify our understanding of how the word “jealous” is being used. Exodus 20:5’s description of God’s jealousy is different as compared to the sin of jealousy outlined in Galatians 5:20.
When we use of the word jealousy, it is meant being envious of someone who has something we do not have. A person might become jealous or envious of a person because of some processions (material) or skills (musical/athletic) they have. Some might be jealous or envious of another person because of his/her beauty.
However, when we look closely at Exodus 20:4-5, we realize that it is not that God is jealous or envious because someone has something He wants that He does not possess. In this case God is talking about being jealous if someone gives something that belongs to Him to another (i.e. making idols and bowing down and worshipping them instead of giving God the worship that belongs to Him alone.)
God classifies it a sin in this command to worship or serve anything other than Him. It is a different use of the word jealous when God says He is a “jealous God”. What He is jealous for belongs to Him, worship and service belongs to Him alone, and are to be given to Him alone. As such, God is rightly jealous when worship, praise, honor, or adoration is given to idols.
This is exactly the same kind of jealous the Apostle Paul described in 2 Corinthians 11:2 “I am jealous for you with a Godly jealousy.” So in summary, it a sin when we desire, or envious, of someone because they have something we desire. On the other hand, the jealousy God portrays in Exodus 20 is appropriate since He is being jealous for something that rightfully belongs to Him.
[Answered by George Mah]