The repeated refrain of Psalm 107, which begins Book V of the Psalter, expresses a desire for people to thank God for His repeated mercy and deliverance in rescuing them from their troubles. With this psalm, Book V in fact opens on much the same note as the previous book closes. Indeed, the first words of Psalm 107 are the same as the first words of Psalm 106—taken from David's psalm in 1 Chronicles 16 (wording further emphasized in Psalm 136).
As the Zondervan NIV Study Bible comments in its introductory note on Psalm 107: "In its recitational style the psalm is closely related to Psalm 104-106, and in its language to Psalm 105-106. For that reason it has been seriously proposed that with these last two psalms it forms a trilogy from the same author. Whether or not this is so, its affinity with the preceding psalms strongly suggests that it was associated with them before the insertion of a Book division between Psalm 106 and 107 and that it was intended to conclude the little series, Psalm 104-107 [or perhaps 103-107 since 103 introduces the theme of God's benefits, including the satisfaction of His people with good (Psalms 103:5 Psalms 103:5Who satisfies your mouth with good things; so that your youth is renewed like the eagle's.
American King James Version×; Psalms 107:9 Psalms 107:9For he satisfies the longing soul, and fills the hungry soul with goodness.
American King James Version×)]. Its recital of God's 'wonderful deeds for men' (v. 8)—which climaxes Psalm 105-106—balances the recital of his many wise works in creation (see Psalms 104:2-26 Psalms 104:2-26  Who cover yourself with light as with a garment: who stretch out the heavens like a curtain:  Who lays the beams of his chambers in the waters: who makes the clouds his chariot: who walks on the wings of the wind:  Who makes his angels spirits; his ministers a flaming fire:  Who laid the foundations of the earth, that it should not be removed for ever.  You covered it with the deep as with a garment: the waters stood above the mountains.  At your rebuke they fled; at the voice of your thunder they hurried away.  They go up by the mountains; they go down by the valleys to the place which you have founded for them.  You have set a bound that they may not pass over; that they turn not again to cover the earth.  He sends the springs into the valleys, which run among the hills.  They give drink to every beast of the field: the wild asses quench their thirst.  By them shall the fowls of the heaven have their habitation, which sing among the branches.  He waters the hills from his chambers: the earth is satisfied with the fruit of your works.  He causes the grass to grow for the cattle, and herb for the service of man: that he may bring forth food out of the earth;  And wine that makes glad the heart of man, and oil to make his face to shine, and bread which strengthens man's heart.  The trees of the LORD are full of sap; the cedars of Lebanon, which he has planted;  Where the birds make their nests: as for the stork, the fir trees are her house.  The high hills are a refuge for the wild goats; and the rocks for the conies.  He appointed the moon for seasons: the sun knows his going down.  You make darkness, and it is night: wherein all the beasts of the forest do creep forth.  The young lions roar after their prey, and seek their meat from God.  The sun rises, they gather themselves together, and lay them down in their dens.  Man goes forth to his work and to his labor until the evening.  O LORD, how manifold are your works! in wisdom have you made them all: the earth is full of your riches.  So is this great and wide sea, wherein are things creeping innumerable, both small and great beasts.  There go the ships: there is that leviathan, whom you have made to play therein.
American King James Version×) and his benevolent care over the animal world (see Psalms 104:27-30 Psalms 104:27-30  These wait all on you; that you may give them their meat in due season.  That you give them they gather: you open your hand, they are filled with good.  You hide your face, they are troubled: you take away their breath, they die, and return to their dust.  You send forth your spirit, they are created: and you renew the face of the earth.
American King James Version×). The editors may have inserted a Book division between Psalm 106 and 107 with a view to a fivefold division of the Psalter"—that is, to parallel the Pentateuch in the scriptural reading cycle.
Verses 2-3 mentions the redeemed of God gathered from enemy captivity in foreign lands. A hint as to what captivity is intended is perhaps found in verse 16, which says that God "has broken the gates of bronze, and cut the bars of iron in two." Very similar language is found in Isaiah 45:1-2 Isaiah 45:1-2  Thus said the LORD to his anointed, to Cyrus, whose right hand I have held, to subdue nations before him; and I will loose the loins of kings, to open before him the two leaved gates; and the gates shall not be shut;
 I will go before you, and make the crooked places straight: I will break in pieces the gates of brass, and cut in sunder the bars of iron:
American King James Version×, which describes the fall of Babylon to the Persian emperor Cyrus: "Thus says the LORD says to His anointed, to Cyrus, whose right hand I have held—to subdue nations before him and loose the armor of kings, to open before him the double doors, so that the gates will not be shut: I will go before you...I will break in pieces the gates of bronze and cut the bars of iron" (Isaiah 45:1-2 Isaiah 45:1-2  Thus said the LORD to his anointed, to Cyrus, whose right hand I have held, to subdue nations before him; and I will loose the loins of kings, to open before him the two leaved gates; and the gates shall not be shut;  I will go before you, and make the crooked places straight: I will break in pieces the gates of brass, and cut in sunder the bars of iron:
American King James Version×). Thus Psalms 107:16 Psalms 107:16For he has broken the gates of brass, and cut the bars of iron in sunder.
American King James Version×points to a fulfillment of this passage. God in fact used Cyrus to issue the first decree allowing the Jewish captives in Babylon to return to Judea.
Verses 4-7 describe the Israelite exiles as having wandered in desolation without enough to eat and drink, with God at last giving them a city to dwell in. This probably refers not to resettlement in Jerusalem (which was not a rescue from severe circumstances) but rather to the Jews eventually settling down in Babylonian communities following their initial deportation. Thus the wording of "go to" rather than "come to" a city (verse 7).
It should be observed that there is also a greater spiritual reality here too—as John the Baptist was later commissioned with words taken from this psalm to show God's people the way out of spiritual imprisonment (compare verse 10; Luke 1:79 Luke 1:79To give light to them that sit in darkness and in the shadow of death, to guide our feet into the way of peace.
American King James Version×), evidently through his message of God's Kingdom and call for repentance.
Among other trials the psalmist alludes to are dangers at sea while conducting maritime commerce (Psalms 107:23-30 Psalms 107:23-30  They that go down to the sea in ships, that do business in great waters;
 These see the works of the LORD, and his wonders in the deep.
 For he commands, and raises the stormy wind, which lifts up the waves thereof.
 They mount up to the heaven, they go down again to the depths: their soul is melted because of trouble.
 They reel to and fro, and stagger like a drunken man, and are at their wit's end.
 Then they cry to the LORD in their trouble, and he brings them out of their distresses.
 He makes the storm a calm, so that the waves thereof are still.
 Then are they glad because they be quiet; so he brings them to their desired haven.
American King James Version×)—demonstrating that the psalm does not exclusively concern returning exiles. "No problem is too great for God. This psalm imagines the worse calamities a Jew could think of: homelessness and starvation (verses 4-5), imprisonment (10-12), self-inflicted disease (17-18), and—the ultimate—imminent shipwreck (23-27). Since Israel was landlocked, few Jews had experienced turbulent seas, and thus dreaded them. In all these cases, God was able to rescue those who called for his help" (Zondervan New Student Bible, note on verse 27). When tempest-tossed sailors are at their wits' end, they cry out to God and He delivers them, bringing them to safe havens (verses 27-30). As Psalms 89:9 Psalms 89:9You rule the raging of the sea: when the waves thereof arise, you still them.
American King James Version×states: "He calms the storm, so that its waves are still."
God is sovereign. In response to wickedness, He can turn fruitful land into barren land (verses 33-34). In showing mercy to the poor and their families, He can turn wilderness into desirable acreage for vineyards, cattle and harvests (verses 35-38). Those who are righteous understand that God punishes wickedness through oppression, affliction and sorrow (verses 33, 39-40) and "sets the poor on high" (verse 41). Wonderfully, as part of God's great benefits, His involvement in man's affairs is ultimately for a great purpose—that "they will understand the lovingkindness [hesed, faithful love] of the LORD" (verse 43). That is certainly cause for thanksgiving.