As soon as the king of Ai saw this, he and all his troops, the men of the city, hurried out early in the morning to meet Israel in battle. They moved out to the battleground on the slopes down toward the Jordan. He didn't know that there was an ambush set against him behind the city.
References for Joshua 8:14
Then Joshua and all Israel let themselves be beaten before them. They fled in the direction of the desert.
Next, all the troops who were still in the city were called out to chase them. They chased after Joshua and so let themselves be drawn away from the city.
No one who hadn't gone out after Israel was left in either Ai or Bethel. They left the city wide open and chased after Israel.
The LORD said to Joshua, "Point the dagger in your hand toward Ai, because I will give it into your power." So Joshua pointed the dagger in his hand toward the city.
The ambush quickly rose from its place. As soon as he reached out his hand, it charged. They entered the city and captured it. They set the city on fire at once.
Then the men of Ai turned around. They caught sight of the smoke of the city rising toward the sky. They had no chance to flee one way or the other. The troops who were fleeing toward the desert turned against the pursuit.
Joshua and all Israel saw that the ambush had captured the city and that the smoke of the city was rising. So they turned and struck down the men of Ai.
When other Israelites came out of the city to confront them, the men of Ai were caught in the middle. Some Israelites were on one side of them and some on the other. The Israelites struck them down until there was no one left to escape.
But they seized the king of Ai alive and brought him to Joshua.
Israel finished killing the entire population of Ai that had chased them out into the open wasteland. All of them were finished off without mercy. Then all Israel went back to Ai and struck it down without mercy.
Twelve thousand men and women died that day, all the people of Ai.
Joshua didn't pull back the hand that was stretched out holding a dagger until he had wiped out the whole population of Ai as something reserved for God.
However, Israel did take the cattle and other booty of that city as plunder for themselves, in agreement with the command that the LORD had given Joshua.
Then Joshua burned Ai. He made it a permanently deserted mound. That is still the case today.
He hanged the king of Ai on a tree until evening. At sundown, Joshua gave an order, and they took his body down from the tree. They threw it down at the opening of the city gate. Then they raised over it a great pile of stones that is still there today.