كتاب الإمارة
33  
The Book on Government
(8)
Chapter: The obligation of obeying leaders in matters that do not involve sin, but it is forbidden to obey them in sinful matters
(8)
باب وُجُوبِ طَاعَةِ الأُمَرَاءِ فِي غَيْرِ مَعْصِيَةٍ وَتَحْرِيمِهَا فِي الْمَعْصِيَةِ ‏‏
Sahih Muslim 1838 a

It has been narrated on the authority of Yahya b. Husain who learnt the tradition from his grandmother. She said that she heard the Prophet (ﷺ) delivering his sermon on the occasion of the Last Pilgrimage. He was saying:

If a slave is appointed over you and he conducts your affairs according to the Book of Allah, you should listen to him and obeey (his orders).
حَدَّثَنَا مُحَمَّدُ بْنُ الْمُثَنَّى، حَدَّثَنَا مُحَمَّدُ بْنُ جَعْفَرٍ، حَدَّثَنَا شُعْبَةُ، عَنْ يَحْيَى بْنِ حُصَيْنٍ، قَالَ سَمِعْتُ جَدَّتِي، تُحَدِّثُ أَنَّهَا سَمِعَتِ النَّبِيَّ صلى الله عليه وسلم يَخْطُبُ فِي حَجَّةِ الْوَدَاعِ وَهُوَ يَقُولُ ‏ "‏ وَلَوِ اسْتُعْمِلَ عَلَيْكُمْ عَبْدٌ يَقُودُكُمْ بِكِتَابِ اللَّهِ فَاسْمَعُوا لَهُ وَأَطِيعُوا ‏"‏ ‏.‏
Reference : Sahih Muslim 1838a
In-book reference : Book 33, Hadith 56
USC-MSA web (English) reference : Book 20, Hadith 4528
  (deprecated numbering scheme)