The Messenger of Allah ﷺ said while delivering a sermon, "When one of you arrives while the imam is giving the sermon, or has already come out, let him pray two rakahs."
حدثنا آدم، قال أخبرنا شعبة، أخبرنا عمرو بن دينار، قال سمعت
قال رسول الله ﷺ وهو يخطب إذا جاء أحدكم والإمام يخطب أو قد خرج فليصل ركعتين.