The setting is the Battle of the Trench, when armies of the Meccan tribes besieged Medina. The early biographies report that Qurayza, who had a pact to help defend the city, were drawn into talks with the besiegers. After the siege failed, they surrendered on the terms that Sa'd ibn Mu'adh, a leader of their old allies, would judge them. In Sahih al-Bukhari, Sa'd judged that the fighters be killed and the families be taken captive, and the Prophet ﷺ said, 'You have judged by the judgment of the King', which another narration gives as 'by the judgment of Allah'. The Quran refers to the event in the chapter of the Confederates.
Muslims hold that it was a judgment for treason in wartime, according to the law of war of that age; a comparable rule appears in the Torah (Deuteronomy 20) for a besieged city that refuses peace. It was not a punishment for being Jewish: the Prophet ﷺ visited a sick Jewish boy who served him, and stood up when a Jewish funeral passed, saying, 'Is it not a soul?' Both reports are in Sahih al-Bukhari. Historians disagree about the details, including how many were executed, since our knowledge of the event comes mainly from the early biographies, which were compiled some time later, and the report in Sahih al-Bukhari gives no numbers.
This is one of the hardest episodes of the early period, and it is right to say so. Many Muslim scholars say that it is a historical ruling for a specific case, and not a template, and that the Quran and the Prophet's example command justice and kindness to Jews and Christians who live peacefully.