Repentance and reliance on the mercy of God were David’s responses to his sin and are our proper responses to our sin as well.
This psalm teaches us that the worst of sinners who repent and rely on God’s mercy can experience sweet forgiveness, restored joy, renewed spirit, cleansed heart and nearness to God. This is only because God is merciful and gracious, loving and forgiving.
“The function of this psalm, as a song sung by the entire congregation, is to shape their hearts so that they feel this at the deepest level, lest they ever presume upon the grace of God.” - C. John Collins, ESV Study Bible Notes, p.1000