That is one of my favorite verses. I know, I say that often. Out of so many, it really is in the top third. It was written by David, King of Israel. it happened within a year after his meeting Bathsheba, the married woman David took from her husband, Uriah. Go ahead and look up the story in 2 Samuel 11 and 12. Anyone wishing to defend their own adultry should read the result of David's.
Psalm 51 is his confession, his pain, his hope for the future. It is labeled: 'To the chief Musician, A Psalm of David, when Nathan the prophet came unto him, after he had gone in to Bathsheba." In the first fourteen verses of 2 Samuel 12, the prophet Nathan confronts David with an analogy of David's sin and the depth of the sin angers David. Nathan states the guilty party:
And Nathan said to David, Thou art the man. (2 Samuel 12:7a KJV)
Nathan continues, listing what God has done for David throughout his life. David acknowledges:
And David said unto Nathan, I have sinned against the LORD. (2 Samuel 12:13 KJV)
Although the "why" isn't in Psalm 51, his prayer acknowledges the sin:
Have mercy upon me, O God, according to thy lovingkindness: according unto the multitude of thy tender mercies blot out my transgressions. Wash me throughly from mine iniquity, and cleanse me from my sin. For I acknowledge my transgressions: and my sin is ever before me. Against thee, thee only, have I sinned, and done this evil in thy sight: that thou mightest be justified when thou speakest, and be clear when thou judgest. (Psalms 51:1-4 KJV)
Only God had laws given through Moses that made powerful men guilty of taking what they want from others. When men forget God, they have always done what is written twice in the book of Judges:
In those days there was no king in Israel, but every man did that which was right in his own eyes. (Judges 17:6 KJV)
In those days there was no king in Israel: every man did that which was right in his own eyes. (Judges 21:25 KJV)
David had seen what powerful rulers did with women. He was in a powerful position. He ignored God's law, and took what he wanted - then attempted to cover up his sinfulness. That's what sin is - ignoring God and His laws meant to protect all men.
David wanted God's forgiveness:
Purge me with hyssop, and I shall be clean: wash me, and I shall be whiter than snow. Make me to hear joy and gladness; that the bones which thou hast broken may rejoice. Hide thy face from my sins, and blot out all mine iniquities. (Psalms 51:7-9 KJV)
This is the desire of any sinner when becoming aware we've fallen short of being what God had in mind. When we realize there is a huge separation between mankind as a whole, we as individuals, and God. Only God can span that separation with what David describes in verse 12:
Create in me a clean heart, O God; and renew a right spirit within me. Cast me not away from thy presence; and take not thy holy spirit from me. Restore unto me the joy of thy salvation; and uphold me with thy free spirit. Then will I teach transgressors thy ways; and sinners shall be converted unto thee. (Psalms 51:10-13 KJV)
Let me assure you, when we realize the joy of our salvation, we do want to share that knowledge with others, and sinners will be converted unto Him. We do desire strongly to praise Him:
Deliver me from bloodguiltiness, O God, thou God of my salvation: and my tongue shall sing aloud of thy righteousness. O Lord, open thou my lips; and my mouth shall shew forth thy praise. For thou desirest not sacrifice; else would I give it: thou delightest not in burnt offering. The sacrifices of God are a broken spirit: a broken and a contrite heart, O God, thou wilt not despise. Do good in thy good pleasure unto Zion: build thou the walls of Jerusalem. Then shalt thou be pleased with the sacrifices of righteousness, with burnt offering and whole burnt offering: then shall they offer bullocks upon thine altar. (Psalms 51:14-19 KJV)
See those bolded parts? God does not require sacrifices. David knew this, yet made such sacrifices. Eventually, God did what Abraham explained to Isaac:
And Abraham said, My son, God will provide himself a lamb for a burnt offering: so they went both of them together. (Genesis 22:8 KJV)
Somehow I've come to believe that Abraham did not say these words as a prophecy, but only as a trusting man that God would provide what was needed, which He did as Jesus, Christ. Because He did, we can understand David's joy in God's salvation, and his guilt in feeling castaway. God forgives and restores our joy.
We will have to live with the consequences of the sinfulness. David and his family certainly did. Easiest way to avoiod such consequences is to become familiar with God, His laws, and His salvation. All are part of Jesus' doctrine (see - I can carry a subject for more than a couple of days!)