The Messenger of Allah ﷺ said, "The imam is appointed only to be followed. So when he says the takbir, say the takbir. When he bows, bow. When he says, 'Allah hears the one who praises Him,' say, 'Our Lord, to You belongs all praise.' When he prostrates, prostrate. And when he prays sitting, all of you pray sitting."
قال رسول الله ﷺ: إنما جعل الإمام ليؤتم به، فإذا كبر فكبروا، وإذا ركع فاركعوا، وإذا قال: سمع الله لمن حمده فقولوا: ربنا ولك الحمد، وإذا سجد فاسجدوا، وإذا صلى جالسا فصلوا جلوسا أجمعون.
Bukhari related it in the Book of Adhan (734), and Muslim in the Book of Prayer (414), both through the route of Abu Zinad, from Araj, from Abu Hurairah, who mentioned the same. The two sheikhs also related it, Bukhari (722) and Muslim, through the route of Abdul-Razzaq, who said, Mamar informed us, from Hammam, from Abu Hurairah, and in it, "The imam is appointed only to be followed, so do not differ from him..." He added at the end of the hadith, "Straighten the rows in prayer, for straightening the rows is part of the excellence of prayer." Muslim did not give the wording of the hadith himself, but instead referred back to the earlier wording.