Historical commentary by Matthew Henry. The wording and interpretations below belong to the original source.
The shaking of a tree (they say) makes it take the deeper and faster root. The attempt of David's enemies to discourage his confidence in God engages him to cleave so much the more closely to his first principles, and to review them, which he here does, abundantly to his own satisfaction and the silencing of all temptations to infidelity. That which was shocking to his faith, and has been so to the faith of many, was the prosperity of wicked people in their wicked ways, and the straits and distresses which the best men are sometimes reduced to: hence such an evil thought as this was apt to arise, Surely it is vain to serve God, and we may call the proud happy. But, in order to stifle and shame all such thoughts, we are here called to consider,
I. That there is a God in heaven: The Lord is in his holy temple above, where, though he is out of our sight, we are not out of his. Let not the enemies of the saints insult over them, as if they were at a loss and at their wits' end: no, they have a God, and they know where to find him and how to direct their prayer unto him, as their Father in heaven. Or, He is in his holy temple, that is, in his church; he is a God in covenant and communion with his people, through a Mediator, of whom the temple was a type. We need not say, "Who shall go up to heaven, to fetch us thence a God to trust to?" No, the word is nigh us, and God in the word; his Spirit is in his saints, those living temples, and the Lord is that Spirit.
II. That this God governs the world. The Lord has not only his residence, but his throne, in heaven, and he has set the dominion thereof in the earth ( Job xxxviii. 33 ); for, having prepared his throne in the heavens, his kingdom ruleth over all, Ps. ciii. 19 . Hence the heavens are said to rule, Dan. iv. 26 . Let us by faith see God on this throne, on his throne of glory, infinitely transcending the splendour and majesty of earthly princes—on his throne of government, giving law, giving motion, and giving aim, to all the creatures—on his throne of judgment, rendering to every man according to his works—and on his throne of grace, to which his people may come boldly for mercy and grace; we shall then see no reason to be discouraged by the pride and power of oppressors, or any of the afflictions that attend the righteous.
III. That this God perfectly knows every man's true character: His eyes behold, his eye-lids try, the children of men; he not only sees them, but he sees through them, not only knows all they say and do, but knows what they think, what they design, and how they really stand affected, whatever they pretend. We may know what men seem to be, but he knows what they are, as the refiner knows what the value of the gold is when he has tried it. God is said to try with his eyes, and his eye-lids, because he knows men, not as earthly princes know men, by report and representation, but by his own strict inspection, which cannot err nor be imposed upon. This may comfort us when we are deceived in men, even in men that we think we have tried, that God's judgment of men, we are sure, is according to truth.