This is not an easy one to explain, but I'll give it a try.
It all starts with God telling David to do a census, you know like the one the U.S. Constitution requires us to do every ten years.
And again the anger of the LORD was kindled against Israel, and he moved David against them to say, Go, number Israel and Judah.
2 Samuel 24:1 Or was it Satan that asked David to do the census, as it says in
1 Chronicles 21.1?
Satan stood up against Israel, and provoked David to number Israel.
Oh well, maybe it was both.
They often work together. In any case, David sent Joab out to take the census, and after 9 months and 20 days, Joab came back with the results: there were 800,000 sword-yielding men in Israel and 500,000 in Judah.
So when they had gone through all the land, they came to Jerusalem at the end of nine months and twenty days. And Joab gave up the sum of the number of the people unto the king: and there were in Israel eight hundred thousand valiant men that drew the sword; and the men of Judah were five hundred thousand men. 2 Samuel 24.9
Or was it was 1,100,000 and 470,000 men in Israel and Judah, as it says in
1 Chronicles 21:5?
Joab gave the sum of the number of the people unto David. And all they of Israel were a thousand thousand and an hundred thousand men that drew sword: and Judah was four hundred threescore and ten thousand men that drew sword.
Whichever it may have been, either is comparable to the number of active duty soldiers in the U.S. military today. Not bad for small tribal kingdom in 1000 BCE!
After the census, David decided that he had done something wrong, which is weird since he had only taken a census that God told him to take.
David's heart smote him after that he had numbered the people. And David said unto the LORD, I have sinned greatly in that I have done. 2 Samuel 24.10
And God was angry, too, at least that's what the prophet Gad told him. Gad said God offered him three choices:
1. Seven years of famine (or three years if you believe the story in
1 Chronicles 21),
2. Three months of losing battles,
3. Or Three days of pestilence.
When David was up in the morning, the word of the LORD came unto the prophet Gad ... saying ... Thus saith the LORD, I offer thee three things; choose thee one of them, that I may do it unto thee. ... Shall seven years of famine come unto thee in thy land? or wilt thou flee three months before thine enemies, while they pursue thee? or that there be three days' pestilence in thy land? 2 Samuel 24.11-13
David couldn't decide, so God decided for him. God chose the three days of pestilence, thereby killing 70,000 men, which would mean at least a couple hundred thousand people (since only men count to God).
So the LORD sent a pestilence upon Israel from the morning even to the time appointed: and there died of the people from Dan even to Beersheba seventy thousand men. 2 Samuel 24.15
But God was still pissed off, even after he finished killing a couple hundred thousand people in the pestilence. So he sent an angel to destroy the city of Jerusalem. But before the angel destroyed the city, God "repented him of the evil" that he intended to do and told him to stop.
When the angel stretched out his hand upon Jerusalem to destroy it, the LORD repented him of the evil, and said to the angel that destroyed the people, It is enough: stay now thine hand. 2 Samuel 24.16
When David saw the angel that was still killing people, he said, "I've sinned, but what have these people done?" A good question, that God, of course, completely ignores.
David spake unto the LORD when he saw the angel that smote the people, and said, Lo, I have sinned, and I have done wickedly: but these sheep, what have they done? 2 Samuel 24.17
Finally, Gad tells David to buy some land, make an altar, and kill some animals to make God quit killing people. So David buys some land for 50 shekels of silver (or 600 shekels of gold if you believe the story in 1 Chronicles 21), sets up an altar, and kills some animals for God.
And God finally stopped killing people.
Gad came that day to David, and said unto him, Go up, rear an altar unto the LORD in the threshingfloor of Araunah the Jebusite ... So David bought the threshingfloor and the oxen for fifty shekels of silver. And David built there an altar unto the LORD, and offered burnt offerings and peace offerings. So the LORD was intreated for the land, and the plague was stayed from Israel. 2 Samuel 24.18-25
God's next killing: Solomon carries out the deathbed wish of David by having Joab and Shimei murdered