"Jealousy" in colloquial English, means either (1) indignation in response to infidelity, or (2) covetousness of the belongings of others. We can immediately eliminate the second case, because God cannot be covetous; everything is his.
(Psalm 50:12) “If I were hungry I would not tell you, for the world is Mine, and all it contains."
This jealousy that is attributed to God must be of the first kind then. Consider the following passages,
(Proverbs 6:34 NASB), For jealousy arouses a husband's fury, and he will show no mercy when he takes revenge.
(Deuteronomy 4:23-24 NASB) So watch yourselves, that you do not forget the covenant of the Lord your God which He made with you, and make for yourselves a graven image in the form of anything against which the Lord your God has commanded you. For the Lord your God is a consuming fire, a jealous God.
(Zechariah 8:2) “Thus says the Lord of hosts, ‘I am exceedingly jealous for Zion, yes, with great wrath I am jealous for her.’
The first passage demonstrates a man's jealousy, which is an act of the flesh that leads to fury and revenge. The second and third demonstrate God's jealousy. It appears that both God and men have the ability to become jealous. So, what is special about God's jealousy?
Notice immediately that God's jealousy is uniquely for his chosen people. Consider the context of the passage you quoted,
(Exodus 20:4-6 NIV) “You shall not make for yourself an idol, or any likeness of what is in heaven above or on the earth beneath or in the water under the earth. You shall not worship them or serve them; for I, the Lord your God, am a jealous God, visiting the iniquity of the fathers on the children, on the third and the fourth generations of those who hate Me, 6 but showing lovingkindness to thousands, to those who love Me and keep My commandments.
Notice that both this passage and the previous passage from Deuteronomy discuss the worship of false gods. God has commanded the Isrealites to worship no other gods but him, and they have made a covenant with him that he would be their God, and they would be his people. The people of Israel, however, are unfaithful to God and undertake worship of other gods. Throughout the prophets, God uses the symbol of marital infidelity to discipline Israel for her tendency to worship idols and the gods of other nations.
(Jeremiah 3:20) But like a woman unfaithful to her husband, so you, Israel, have been unfaithful to me," declares the LORD.
So, while covetousness or envy is an unrighteous desire to obtain what belongs to another, the jealousy that is referred to in Exodus 20:4-5 is indignation on God's part in response to unrighteous infidelity on the part of God's covenanted people. Israel willfully entered into a covenant with God as a wife willfully enters into a covenant with her husband. God is not desiring something that is not his, but instead he is angry and distressed by Israel's unfaithfulness to him. The nation of Israel belongs to God because he created it,
(Isaiah 43:1 NASB) But now, thus says the Lord, your Creator, O Jacob, And He who formed you, O Israel, “Do not fear, for I have redeemed you; I have called you by name; you are Mine!
And because- more importantly for this question- they covenanted themselves with him.
(Exodus 19:5-6 NASB) Now then, if you will indeed obey My voice and keep My covenant, then you shall be My own possession among all the peoples, for all the earth is Mine; and you shall be to Me a kingdom of priests and a holy nation.’
Let us then consider the passage from Galatians,
(Galatians 5:19-20 NASB) Now the deeds of the flesh are evident, which are: immorality, impurity, sensuality, idolatry, sorcery, enmities, strife, jealousy, outbursts of anger, disputes, dissensions, factions,
In this passage, Paul lists "jealousy" among the unrighteous deeds of the flesh. But Paul also says of God, in the context of consuming wine sacrificed to false gods, that is, to demons,
(1 Corinthians 10:21-22 NASB) You cannot drink the cup of the Lord and the cup of demons; you cannot partake of the table of the Lord and the table of demons. Or do we provoke the Lord to jealousy? We are not stronger than He, are we?
Notice that in this context Paul, who also wrote the passage in Galatians, warns that associating ourselves with idol worship provokes God to jealousy- the very same thing that the prophets had been saying.
It is important to consider that in many instances, God's actions are justified, even when the same actions taken by men would be considered unrighteous. God is Sovereign of Sovereigns, and so he is not subject to the commandments he has given us to follow. When a mother tells her son not to touch the matches, is it then wrong for her to light the stove to cook him a meal? Of course not, but she uses the matches to light the stove because she loves him and in her love provides a meal for him. Paul includes jealousy in his list of fleshly deeds, but we know well that God is greater than us, that he does not suffer from the same tendencies to act according to the flesh, and the expression of God's jealousy is righteous and justified. If it were not so, he would not say "I am a jealous God."