During the lifetime of the Prophet Muhammad (PBUH), in general, everything was referred back to him, and thus was considered as part of the Sunnah. But upon his death, in 632 ce, although the foundations of all Islamic legislation was complete, of course various nuances and unprecedented situations arose which needed clear rulings. This began immediately with issues such as how to wash the Prophet's body, where to bury him, what style of grave to dig, and of course who should be the leader of the Muslims after his death.
With each situation that arose, counsel was taken amongst the most well-learned and righteous, based on the Quran and the Sunnah that they had not only memorised, but also lived and experienced as direct companions of the Prophet (PBUH). This process was all in line with what the Prophet (PBUH) had taught them before his passing.
This same process continues to be used amongst the righteous and well learned Islamic scholars till this very day.