Second Samuel 16:1517:23 tells us that Absalom took Jerusalem and ruled there.
As David had requested, his friend Hushai pretended to serve Absalom (16:15-19).
Ahithophel gave an evil counsel for Absalom to go in unto his father's concubines for the strengthening of all those who followed him (vv. 20-23).
In 17:1-4 Ahithophel gave another inhuman counsel for Absalom to pursue after his father and kill him.
Verses 5 through 22 reveal that Hushai defeated the counsel of Ahithophel according to God's ordination.
Hushai annulled Ahithophel's inhuman counsel (vv. 5-14). Regarding this, verse 14 says, "Absalom and all the men of Israel said, The counsel of Hushai the Archite is better than the counsel of Ahithophel; for Jehovah had ordained the defeat of the good counsel of Ahithophel to the intent that Jehovah might bring evil upon Absalom."
Hushai defeated the counsel of Ahithophel with the help of the priests and their sons, who told David how to escape from his son Absalom (vv. 15-22).
When Ahithophel saw that his counsel was not followed, he went back home and hanged himself (v. 23).