2 Samuel 17:14 Absalom and all the men of Israel said, “The advice of Hushai the Arkite is better than that of Ahithophel.” For the Lord had determined to frustrate the good advice of Ahithophel in order to bring disaster on Absalom.

15 Hushai told Zadok and Abiathar, the priests, “Ahithophel has advised Absalom and the elders of Israel to do such and such, but I have advised them to do so and so. 16 Now send a message at once and tell David, ‘Do not spend the night at the fords in the wilderness; cross over without fail, or the king and all the people with him will be swallowed up.’”

17 Jonathan and Ahimaaz were staying at En Rogel. A female servant was to go and inform them, and they were to go and tell King David, for they could not risk being seen entering the city. 18 But a young man saw them and told Absalom. So the two of them left at once and went to the house of a man in Bahurim. He had a well in his courtyard, and they climbed down into it. 19 His wife took a covering and spread it out over the opening of the well and scattered grain over it. No one knew anything about it.

20 When Absalom’s men came to the woman at the house, they asked, “Where are Ahimaaz and Jonathan?”

The woman answered them, “They crossed over the brook.”[c] The men searched but found no one, so they returned to Jerusalem.

21 After they had gone, the two climbed out of the well and went to inform King David. They said to him, “Set out and cross the river at once; Ahithophel has advised such and such against you.” 22 So David and all the people with him set out and crossed the Jordan. By daybreak, no one was left who had not crossed the Jordan.

23 When Ahithophel saw that his advice had not been followed, he saddled his donkey and set out for his house in his hometown. He put his house in order and then hanged himself. So he died and was buried in his father’s tomb.

c. 2 Samuel 17:20 Or “They passed by the sheep pen toward the water.”

With all this action.... you would think the commentaries wouldn't be so preachy.

OK... here's the review. David is on the lamb because his son, Absalom wants the throne and after four years of campaigning he has run David out of town. Now David left town because he knew what Absalom was up to and he didn't want the innocent citizens of Jerusalem to be murdered. If Absalom took Jerusalem by force, then he would be obliged to kill everyone in town. This way, Absalom has to go chasing the battle and the people in town are safe.

Now Hushai, working undercover for David, had given Absalom some advice on how to get to David. Absalom chose the advice of Hushai over that of Ahithophel. Ahithophel, had been one of David's trusted advisers as well, and his counsel had been better but God had other plans. [This is where the commentaries went preachy].

Now to these verses. Hushai's plan had been chosen and he had to get word out to David. David had to move everyone and everything across the Jordan. Two men, Ahimaaz and Jonathan, were chosen to get the word through to David. They were spotted on the way out of town and they had to hide in a well until things were safe. The woman in the house... camaflouged the well opening with a blanket and seeds so they were not discovered. They were able, then, to escape unseen and warn David.

This is from biblestudytools.com.

The court was that of the house, and the well an empty cistern. All the houses of the better class are furnished with such reservoirs. Nothing could more easily happen than that one of these wells, in consequence of a deficiency of water, should become dry and it would then answer as a place of retreat, such as David's friends found in the man's house at Bahurim. The spreading of a covering over the well's mouth for the drying of corn is a common practice.

Meanwhile, back in Jerusalem, Ahithophel could see his future growing dark. Unlike Hushai, who was sent to work undercover, Ahithophel had chosen to go with Absalom. He really thought Absalom might be the new king.... and he would have seen this as a career advancement. Now that Absalom chose Hushai's plan, which would probably fail, it was pretty obvious to Ahithophel that he had backed the wrong guy and rather than go through all the humiliation and pain.... of giving himself up to David.... he would rather do himself in. That's what he did.

This is from blueletterbible.org.

Ahithophel did not kill himself over hurt feelings because his counsel was rejected. Instead, he was wise enough to know that under Hushai's plan Absalom would fail and Ahithophel would be implicated in the conspiracy. He knew all would be lost.

Spies and suicide.... and they say reading the Bible is boring.