David wrote this Psalm after the prophet Nathan had confronted him with his sin in committing adultery with Bathsheba and killing Uriah (her husband). It is also significant that it comes after Psalm 50 which focused on the attitudes of the heart.
David relies on God's mercy and compassion. We must use the same grounds in seeking forgiveness from God, and the depths of God's mercy and compassion are shown in His sending His own Son, Christ, to the cross to die for us.
David is aware of his sin. Unless we confess and acknowledge our sins, there is no hope for us. Moreover, we have sinned against God. Now someone might respond here that it is actually Uriah who has been sinned against. How does God come in to it? Well, we all belong to God. If someone hurt one of my children, then they have sinned against me. Whenever we sin against someone else we have sinned against God. God counts each human being as precious (including the ones we tend to count as worthless!).
David then talks about his being sinful from the very beginning. This is the doctrine of original sin. We have all been infected or affected by Adam's sin. When we sin we do not become sinners so much as demonstrate that we are sinners.
The he refers to God teaching him in the womb. How does this work? Well I don't know, but God's dealings with us go deeper than we imagine.
Next David calls on God to cleanse him. Sin soils us, it corrupts us, but the good news is that through the blood of Jesus we can be cleansed. It is possible for us to be set free from sin, it is possible for our joy to be renewed.
"Create in me a pure heart". We need a new heart. We do not need a new set of rules, we do not need some new spiritual practices, we need a new heart, and that is what the Holy Spirit gives to us. We are being renewed from the inside out.
Once restored we can teach transgressors God's ways. Now this raises a very difficult problem. How should we react if some leader or other sins in some way and then "repents". The first thing to realise is that there is no simple answer. There are two extremes: on the one hand we can say they have sinned against God and are no longer worthy of serving Him as a leader; on the other we can say God is a God of grace so they can be restored to their position of leadership. If we always adopt the first then we will neglect God's grace and reject some people who should not be rejected. If we adopt the second then we will cheapen grace and allow people to lead who should not be leading.
So what should we do? Well first of all look at the case of David closely. He had paid a heavy price for his sin with the loss of his son. The notion that says that grace means sin has no effect is utterly wrong. Secondly look at David's heart. He is clearly contrite and humble. Without these there is little evidence of true repentance.
So there are no simple answers, and we will get it wrong from time to time, but we need to use wisdom in deciding these matters.
A broken and contrite heart are what God desires. We need to admit our utter dependence upon Him.
Finally David prays for the prosperity of Jerusalem. A further sign of true repentance is concern beyond ourselves.