A Muslim person must listen and obey concerning what he likes and hates, unless he is ordered to commit a sin. If he is ordered to commit a sin, then he should neither listen nor obey
Authentic hadithعن ابن عمر رضي الله عنهما مرفوعاً: «على المرء المسلم السمع والطاعة فيما أحب وكَرِهَ، إلا أن يُؤمر بمعصية، فإذا أُمِرَ بمعصية فلا سمع ولا طاعة».
Ibn ‘Umar (may Allah be pleased with him) reported that the Prophet (may Allah's peace and blessings be upon him) said: “A Muslim person must listen and obey concerning what he likes and hates, unless he is ordered to commit a sin. If he is ordered to commit a sin, then he should neither listen nor obey.”
Explanation
According to this Hadīth, it is mandatory to listen to and obey the ruler, whether or not one likes his commands. Only if one is ordered to commit a sin, he is not under obligation to listen and obey concerning this specific order.
Source · Narrated by Bukhari & Muslim
From the Hadith Encyclopedia (hadeethenc.com).