The psalmist, King David, makes a claim that the modern Westerner, steeped in the feel-goodism of political correctness and postmodern aversion to judgmentalism, flinches from, questioning whether it is even properly Christian. Such people would cite the words of Jesus in Matthew 5:44-45, saying that we are to love our enemies and do good for them despite their insults and persecutions because our Father in heaven does good to both the evil and the good. While these verses may seem to be in direct contradiction to each other, they are, in fact, complementary, deepening our understanding of God's way.
Critics commonly make the mistake of "proof-texting," that is, considering a text as "proof" of a biblical truth without taking context and other passages into consideration. Plucking this verse alone out of Psalm 139 and giving it ultimate credence would be proof-texting at its worst. In this case, as in many cases of supposed contradictions, context is key to understanding David's thought, expressed in such absolute, impassioned terms.
Verse 21 falls near the end of a long prayer to God in which David relates in various ways that he realizes how well God knows him. That is how he opens the psalm, giving us a very broad hint at its subject: "O LORD, You have searched me and known me" (verse 1). God knew everything there was to know about the king of Israel, including his every thought and word, and in fact, He had made him, designed him, to be that way (see verses 13-16)! Moreover, God was always with him, and if David had even tried to flee from Him, there is no way that he could have escaped (verses 7-12)!
In verse 17, he begins to bring his thoughts around to the idea he expresses in verses 21-22 about hating those who hate God. He opens this section of the psalm with an exclamation about how valuable he considers God's thoughts—His revelation of Himself and His way of life—to be. Thinking about how precious God's truth is leads him to react strongly against those who oppose God and all the good that His Word can do. He asks God to "slay the wicked" (verse 19) for their bloodthirsty fight against Him—and God's people, whose blood is being shed.
David's words in verses 21-22, then, expressing his perfect or complete hatred against God's enemies, are a declaration of loyalty and devotion to God's cause. If they opposed God, he would oppose them. He was all in. So he says, "Search me, O God, and know my heart" (verse 23). He had no reservations about his commitment to God's side, knowing that such devotion would lead to "the way everlasting" (verse 24).
We also need to understand the Hebrew word behind "hate"; it is not as absolute as we tend to consider it. The word is sânê, and its meanings range from real hatred—the intense, visceral emotion of antagonism against another—to be set against or intolerant of another. In this case, David's uncompromising loyalty to God excludes any kind of tolerance of those who have proclaimed themselves as God's enemies. So, in this case, David's hatred of those who hate God is an implacable rejection of them; he has set himself against them because they are actively hostile to God. Thus, his "hatred" is, not malevolence, but in actuality zeal for God, a righteous, vehement devotion to his sovereign Lord.