Psalm 51 was written by David after he had been confronted by the prophet Nathan about committing adultery with Bethsheba. In addition to this sexual sin, David caused the death of Uriah, Bethsheba's husband. Consider David's attitude toward God as he deals with the guilt of his sin.
"Have mercy on me, O God, according to your unfailing love; according to your great compassion blot out my transgressions. Wash away all my iniquityand cleanse me from my sin. For I know my transgressions, and my sin is always before me. Against you, you only, have I sinned and done what is evil in your sight; so you are right in your verdict and justified when you judge. Surely I was sinful at birth, sinful from the time my mother conceived me. Yet you desired faithfulness even in the womb; you taught me wisdom in that secret place.
Cleanse me with hyssop, and I will be clean; wash me, and I will be whiter than snow. Let me hear joy and gladness; let the bones you have crushed rejoice. Hide your face from my sins and blot out all my iniquity.
Create in me a pure heart, O God, and renew a steadfast spirit within me. Do not cast me from your presence or take your Holy Spirit from me. Restore to me the joy of your salvation and grant me a willing spirit, to sustain me.
Then I will teach transgressors your ways, so that sinners will turn back to you. Deliver me from the guilt of bloodshed, O God, you who are God my Savior, and my tongue will sing of your righteousness. Open my lips, Lord,and my mouth will declare your praise. You do not delight in sacrifice, or I would bring it; you do not take pleasure in burnt offerings. My sacrifice, O God, is a broken spirit; a broken and contrite heart you, God, will not despise.
May it please you to prosper Zion, to build up the walls of Jerusalem. Then you will delight in the sacrifices of the righteous, in burnt offerings offered whole; then bulls will be offered on your altar."
David does not seek to defend his actions for they are indefensible (as are our sins). Rather, he seeks mercy from a loving God. David admits his guilt and seeks the cleansing that only God can impart. David understands that his sin is a sin against God and that God's judgment is right and just. David cries out not only for cleansing, but for renewal of the mind and spirit. This is the same renewal spoken of by Paul in Romans 12, "... be transformed by the renewing of your mind..."
Like David, we are to admit our guilt before God. We are to cry out to God for mercy from a just and loving God. We are to seek the cleansing and renewal that ONLY God can do. Then, we can serve as a witness to others about God with a contrite heart.