When the Messenger was informed of this anti-Muslim gathering of confederates (ahzab) through his intelligence service, he consulted his Companions. It was their unanimous view that they should remain in Madina and fight from there. Salman al-Farisi suggested digging a trench around the city. It took 6 days of feverish labor to dig this trench. The Messenger divided the Muslims into groups of ten and told them to compete with each other. It was a hard task, there was not much time, and hunger was rampant. Yet all the Companions worked enthusiastically. In order to not feel the hunger, each fastened a rock around his stomach and recited, while digging:
We are those people who
Took the oath of allegiance to Muhammad;
Therefore we shall fight in the way of God
As long as we live.
By God, if God had not enabled us to,
We would have neither been guided
Nor given alms, nor performed prayers.
Send down unto us calmness and tranquility
And make our feet firm if we confront the enemy!
The Messenger, who dug alongside them with two rocks fastened around his stomach, answered them with the couplet:
O God, the real life is the life of the Hereafter
So, forgive the Helpers and the Emigrants.
While digging the ditch, the Companions unearthed a huge rock that they could not break. Informing the Messenger of this, he began to strike it with his pickaxe. In the light of the resulting sparks, he predicted: "I have been given the keys to Persia; my community will conquer it." He struck the rock a second time and, in the light of the resulting sparks, declared: "God is the Greatest. I have been given the keys to Byzantium. My community will conquer it." 
Madina under threat. The allies advanced against Madina in the hope of destroying the Muslims on an open battlefield. However, when they faced this new strategy, they took the first blow. Numbering around 20,000, they camped near the ditch. The Madinans had no more than 3,000 soldiers. Moreover, the Jewish Banu Qurayza and the Hypocrite fifth columns already had contacted the enemy. As stated in Qur'an 33:12–20, when the Hypocrites first saw the enemy, they were already in a defeatist mood. Not content with disloyalty themselves, they tried to infect others, who made feeble excuses to withdraw. If the enemy could gain entrance, they would betray the city.
The Messenger once again displayed his sagacity and military genius: he kept the soldiers within the city and stationed them so that they could safeguard their homes against possible Banu Qurayza attacks. The most critical moment came when the Banu Qurayza sent a man into Madina to learn the conditions of the Muslim women. However, their hopes were frustrated when this man was killed by Safiyya, the Prophet's aunt.
While the war was continuing with exchanges of arrows and stones, The Messenger engaged in diplomatic attempts to split the Allies. He contacted the Ghatafan's leaders and, offering them peace, urged them to withdraw their people. Nu'aym ibn Mas'ud, an Ally leader who before the battle had come to Madina to sow discord, already was inclining toward Islam. During the battle, he secretly entered Islam and followed the The Messenger's order to stir up the Banu Qurayza. Nu'aym set them against the Quraysh by asserting that the Makkans would abandoned them and so they should withhold their help until the Quraysh gave them hostages. Then he told the Quraysh that the Banu Qurayza would not fulfill their promise and would try to stall by asking for Qurayshi hostages to share their plight in the case of defeat. This stratagem succeeded, and dissension grew among the Allies. 
The Messenger, supported by Sal mountain behind the city, had ordered a narrow point to be made in the trench, as he expected that leading Qurayshi horsemen would try to cross there. It happened as he had expected, and some of the most renowned Qurayshi warriors attempted to cross for single combat with Muslim fighters. Among them were 'Amr ibn 'Abd Wudd, Ikrima ibn Abi Jahl, Hubayra ibn Abi Wahb, Dirar ibn al-Khattab, and Nawfal ibn 'Abd Allah ibn al-Mughira.
Boasting of his strength and fighting ability, 'Amr dismounted from his horse and faced 'Ali, who was ordered by the Messenger to fight him. 'Amr advanced with his sword drawn. He brought his sword quickly against 'Ali, but it caught in 'Ali's shield. 'Ali struck him with such strength that dust rose around them. Then the words Allahu akbar (God is the Greatest) were heard: 'Ali had killed his opponent.  He also killed Dirar, Hubayra, and Nawfal.  No other Qurayshi horsemen or generals could get across at that spot.
The siege lasted 27 days. The Muslims suffered greatly from hunger, cold, unending barrages of arrows and stones, attempts and concentrated assaults to cross the trench, and betrayals and intrigues within Madina. The Qur'an describes this situation as follows:
When they came against you from above and from below, and when your eyes swerved and your hearts reached your throats, while you thought thoughts about God; there it was that the believers were tried, and shaken most mightily. And when the Hypocrites, and those in whose hearts is sickness, said: "God and His Messenger promised us only delusion." And when a party of them said: "O people of Yathrib, there is no abiding here for you, therefore return!" And a party of them were asking leave of the Prophet, saying: "Our houses are exposed"; yet they were not exposed. They desired only to flee. (33:10-13)
After almost 4 weeks, during which the enemy was disheartened by it failure and the believers proved their steadfastness and loyalty, there was a piercing blast of cold wind from the east. The enemy's tents were torn up, their fires were extinguished, and sand and rain beat their faces. Terrified by the portents against them, and already riven by discord, they soon gave up. Hudayfa al-Yamani, sent by the Messenger to spy on the enemy's movements, heard Abu Sufyan shout: "Come on, we're going home!"  The Muslims were victorious by God's help, for hidden forces—the Angels—were helping them: O believers, remember God's blessing upon you when hosts came against you, and we loosed against them a wind, and hosts you didn't see. God sees the things you do (33:9).
The Battle of the Trench was the last Qurayshi attempt to destroy Islam and the Muslims. Following their withdrawal in defeat and humiliation, the Messenger declared: "From this moment we will march upon them; they will no longer be able to raid us."
After the Allies were routed and returned to their homes, the Messenger focused on to the Banu Qurayza, who had betrayed their agreement with the Messenger and allied themselves with the Quraysh. They also had given asylum to the Banu Nadir's leaders, like Huyay ibn Akhtab, who had been expelled from Madina and continued to conspire against the Muslims.
No sooner had the Messenger returned from this battle than Archangel Gabriel came and said: "I have not taken off my coat of mail, and I am going to the Banu Qurayza." The Messenger ordered his Companions to march upon this Jewish tribe, and had his tent pitched opposite their fortresses. He would have forgiven them if they had asked, but they preferred to resist. The Messenger besieged them for 25 days. At last they asked for surrender terms, agreeing that they should submit to Sa'd ibn Mu'adh's judgment, who decreed the sentence according to the Torah. This was the end of the Banu Qurayza's conspiracies, as well as of the Jewish presence in Madina.
Sa'd ibn Mu'adh, a leader of the Ansar, had been wounded in the Battle of the Trench. He prayed: "O God, if I am able to fight once more beside the Messenger, make me live. Otherwise, I am ready to die." He died a martyr shortly after the Jewish conspiracies ended.
 Ibn Hisham, 3:240–2.
 Ibn Hisham, 3:235–6.
 Ibn Kathir, 4:123.
 Ibn Hisham, 3:243.