Looking back to pioneer times or the popular depictions of the Old West, judges were selected based upon the amount of respect the people of a town had for their opinion and common sense. In some cases, the judge may have studied law, either at school or simply by reading law books and specific cases. They did not have to be lawyers and in some communities those with law degrees were not necessarily trusted to have good judgement and a sitting judge who maintained a law office was a rarity.
Over time this has changed. Today most judges have spent time arguing cases. They almost all have a law degree and did time clerking for judges and then practicing law themselves. Smaller towns will have judges who practiced general law, familiar with all types of cases. In larger jurisdictions, specialization is the norm, with separate judges hearing criminal and civil cases or even handling only traffic or family law.
That specialization is also reflected in law firms. Anyone who walks into a los angeles law office will find that they have a team of lawyers who each focus on a different aspect of law. This allows the firm to cover all the bases. There are some firms that are experts in only one area of law. Some offices will proudly recognize those lawyers in their practice who move on to become judges.