2 Samuel 22:31 “As for God, his way is perfect:
The Lord’s word is flawless;
he shields all who take refuge in him.
32 For who is God besides the Lord?
And who is the Rock except our God?
33 It is God who arms me with strength[h]
and keeps my way secure.
34 He makes my feet like the feet of a deer;
he causes me to stand on the heights.
35 He trains my hands for battle;
my arms can bend a bow of bronze.
36 You make your saving help my shield;
your help has made[i] me great.
37 You provide a broad path for my feet,
so that my ankles do not give way.

38 “I pursued my enemies and crushed them;
I did not turn back till they were destroyed.
39 I crushed them completely, and they could not rise;
they fell beneath my feet.
40 You armed me with strength for battle;
you humbled my adversaries before me.
41 You made my enemies turn their backs in flight,
and I destroyed my foes.
42 They cried for help, but there was no one to save them—
to the Lord, but he did not answer.
43 I beat them as fine as the dust of the earth;
I pounded and trampled them like mud in the streets.

44 “You have delivered me from the attacks of the peoples;
you have preserved me as the head of nations.
People I did not know now serve me,
45 foreigners cower before me;
as soon as they hear of me, they obey me.
46 They all lose heart;
they come trembling[j] from their strongholds.

47 “The Lord lives! Praise be to my Rock!
Exalted be my God, the Rock, my Savior!
48 He is the God who avenges me,
who puts the nations under me,
49 who sets me free from my enemies.
You exalted me above my foes;
from a violent man you rescued me.
50 Therefore I will praise you, Lord, among the nations;
I will sing the praises of your name.

51 “He gives his king great victories;
he shows unfailing kindness to his anointed,
to David and his descendants forever.”

h. 2 Samuel 22:33 Dead Sea Scrolls, some Septuagint manuscripts, Vulgate and Syriac (see also Psalm 18:32); Masoretic Text who is my strong refuge
i. 2 Samuel 22:36 Dead Sea Scrolls; Masoretic Text shield; / you stoop down to make
j. 2 Samuel 22:46 Some Septuagint manuscripts and Vulgate (see also Psalm 18:45); Masoretic Text they arm themselves

Bible.org had a good commentary this morning.

All of this is to enable David to pursue his enemies successfully so that they turn and run (verse 38). They do not escape, however, for God enables David to destroy (to pulverize, verse 43) those who oppose him (verses 39-43). Some of David's enemies -- perhaps even many of them -- appear to be fellow Israelites, but his enemies and his allies also include the Gentiles. In the closing verses of the psalm, the Gentiles become more prominent. Delivering David from the contentions of his own people (verse 44), God also strikes fear in the hearts of the nations (the Gentiles). As a result, God not only established David as king over Israel, He kept him as head over the nations. These Gentiles fear David, and if their submission to him is not genuine, they at least feign allegiance to him (verses 44-45). They lose heart and come trembling to him from their fortresses (verse 46).

God is David's refuge and defender. When he calls to Him for help, God hears and helps him. God will move heaven and earth to bring this help to David, though at times He saves him by giving him the strength to oppose and overcome his enemies. Now here is where things get very interesting. Just who are David's enemies? And who are those with whom he will praise God? Self-righteous Jews would have a quick and easy answer: “The Jews are those who are David's friends, who will join him in worshipping God; the Gentiles are the enemies of God, who deserve to be pulverized.” But this is not at all what David says.

David clearly indicates that a number of his enemies are those of his own people (see verse 44a), and that there are those from the nations who submit to him and will worship God with him (see verse 44b). The clearest statement comes in verse 50.

2 Samuel 22:50 Therefore I will praise you, Lord, among the nations;
I will sing the praises of your name

There's a lot of talk about "Nations" this morning. The commentaries seem to be saying that all this talk about "Nations" is actually talk about Gentiles. Most of all, though, David was respected by the gentiles as well. It's one thing to have a king. It's quite another to have a great king that is respected by everyone, Hebrew or Gentile, near and far. David was respected. David admits, states, testifies, in these verses, that he believes God is the reason.