Psalm 54 was written by King David when the Ziphites betrayed him. There were two betrayals by the Ziphites in collusion with Saul, we find it in 1 Samuel 23 and 1 Samuel 26, although according to analysts this psalm is more in line with the betrayal in 1 Samuel 23. So, this psalm is a song of gratitude God for freeing him from the Ziphites.
In the first verses of this Psalm we see David pleading with God to listen to him and free him from his enemies (the Ziphites):
19 Then came up the Ziphites to Saul to Gibeah, saying, Doth not David hide himself with us in strong holds in the wood, in the hill of Hachilah, which is on the south of Jeshimon?
20 Now therefore, O king, come down according to all the desire of thy soul to come down; and our part shall be to deliver him into the king’s hand.
21 And Saul said, Blessed be ye of the Lord; for ye have compassion on me.
1 Samuel 23:19-21
Remember that Saul was chasing David to take his life, but God did not allow it, although those of Zif betrayed him, and the truth is that God listened to David and delivered him from his enemies. We must trust God in times of extreme difficulty, just as David did.
Given God’s victory for his servant, for having freed him from those of Zif and from Saul’s hand, David sang:
5 He shall reward evil unto mine enemies: cut them off in thy truth.
6 I will freely sacrifice unto thee: I will praise thy name, O Lord; for it is good.
Psalm 54:5-6
David speaks here that he would offer God a voluntary sacrifice, that is, not something obligatory, or something he had to do by mere commitment, but because he was really grateful for what God had done for him.
Let us give glory to God and praise for what He has done for us.