The Prophet ﷺ said: When the Imam completes the prayer and sits (for reciting tashahhud), and then becomes defiled (i.e. his ablution becomes void) before he speaks (to someone), his prayer becomes complete. And those who prayed behind him also complete the prayer.
أَنَّ رَسُولَ اللَّهِ ﷺ قَالَ إِذَا قَضَى الإِمَامُ الصَّلاَةَ وَقَعَدَ فَأَحْدَثَ قَبْلَ أَنْ يَتَكَلَّمَ فَقَدْ تَمَّتْ صَلاَتُهُ وَمَنْ كَانَ خَلْفَهُ مِمَّنْ أَتَمَّ الصَّلاَةَ