The gate was shut in order to trap the spies in the city. The king did not know for sure that they had gone back to the river. Possibly, to his mind, the spies had simply pretended to leave, planning on coming back later. By shutting the gates, he would trap the spies in the city if they were still there.
7 So the men pursued after them on the way to the Jordan as far as the fords. And the gate was shut as soon as the pursuers had gone out.