Things are going well for David. But then, one day, Satan "incites" David to "number" Israel, i.e. count them. Take a census.
For the record, Joab is totally against David's plan. But what the king wants, the king gets, so Joab goes out and has everyone in Israel counted. Everyone except Levi and Benjamin's tribes because… well, again, no clue. It just seems like Joab's heart really isn't in it.
God is seriously displeased because… well, we don't actually know why. We'd say God hates counting, but have you read the Book of Numbers lately?
Maybe it's because only God can order a census. Not, y'know, Satan.
David admits to God that he's done a dumb thing and begs forgiveness.
God sends a message to David. He tells him that he can pick one of three punishments. Either Israel has to suffer three years of famine, three months of foreign attacks, or three days of God's wrath and pestilence. Tough choice.
David's in total despair, but he opts for the three days of pestilence.
God plagues the entire country and 70,000 people die. Then God sends an angel to destroy Jerusalem, But at the last minute, he reconsiders.
God orders the angel to stop right when the angel is standing, sword drawn and ready to smite, on a threshing floor that happens to be owned by a guy named Ornan the Jebusite.
David sees the angel standing there (we guess he also happened to be hanging out on a threshing floor).
He begs God to spare the people. After all, he was the one who made the mistake. Why should everyone else suffer?
God's angel tells David to make an altar right there on that threshing floor. Why wait?
So David approaches Ornan to buy the place on the spot. Ornan sees the angel and immediately tells David to go ahead and take the land. He'll throw in some oxen and wheat, too, for good measure. No worries. Ornan will just be leaving now…
Not so fast. David insists that he has to purchase the land for full price. It's the only way to save the entire country.
So Ornan sells the place for 600 gold shekels and David sets up shop.
God is pleased and lets the angel know that's it's OK to put away his sword of destruction.
Because of what happened in this very place, David decides that this site, instead of the place where the tabernacle is currently located, is the exact right place to build a temple. The Chronicler says that he was also traumatized by the angel's sword and was afraid to worship at the tabernacle.