While delivering a sermon, Messenger of Allah ﷺ said, "If anyone of you comes while the Imam is delivering the sermon or has come out for it, he should offer a two rakʿat prayer."
قَالَ رَسُولُ اللَّهِ ﷺ وَهُوَ يَخْطُبُ إِذَا جَاءَ أَحَدُكُمْ وَالإِمَامُ يَخْطُبُ أَوْ قَدْ خَرَجَ فَلْيُصَلِّ رَكْعَتَيْنِ