Imam al-Sha'bi (may Allah have mercy on him) narrates from Qadi Shurayh (may Allah have mercy on him) that Sayyiduna Umar bin al-Khattab (may Allah be pleased with him) wrote to him: When the ruling on a matter is found in the Qur'an, judge according to it, and do not let people turn you away from it. If it is not in the Qur'an, then look into the Sunnah of the Messenger (peace and blessings be upon him); if you find it, judge according to it. If it is in neither the Qur'an nor the Sunnah, then judge according to what the people have agreed upon (ijma'). If neither the Qur'an, the Sunnah nor the opinion of the people is available, then choose whichever course you like: if you wish, exercise your own judgement (ijtihad), then either act on it at once or defer it, though deferring is better.
Hadith Referenceالسنن الكبرى للبيهقي / كتاب آداب القاضي / 20342