Psalm 40's title says it is For the Chief Musician and is A Psalm by David. The first part is a song of thanks. I waited patiently for Yahweh. He turned to me, and heard my cry. He brought me up also out of a horrible pit, out of the miry clay. He set my feet on a rock, and gave me a firm place to stand. He has put a new song in my mouth, even praise to our God. Many shall see it, and fear, and shall trust in Yahweh. Blessed is the man who makes Yahweh his trust.
The middle of the psalm explains what God truly wants. Sacrifice and offering you didn't desire. You have opened my ears. Then I said, Behold, I have come. It is written about me in the book in the scroll. I delight to do your will, my God. Yes, your law is within my heart. In the New Testament, the book of Hebrews applies these words to Jesus, who came to do God's will and offered his own body once for all (Hebrews 10:5-10). David then says he proclaimed glad news of righteousness in the great assembly and did not hide God's faithfulness and salvation.
The last part shows that trouble can return after rescue. For innumerable evils have surrounded me. My iniquities have overtaken me. They are more than the hairs of my head. David prays, Be pleased, Yahweh, to deliver me. Hurry to help me, Yahweh. He asks that those who seek his harm be turned back, and that all who seek God rejoice and say, Let Yahweh be exalted! He ends humbly: But I am poor and needy. May the Lord think about me. You are my help and my deliverer. Don't delay, my God.