The earliest bishops appeared soon after the crucifixion of Jesus, about 50 AD. A crowd of Christian people in a town would just go to some rich Christian man’s house and yell for him to be the bishop. If he agreed, then he was the new bishop and leader of the Christians in that town.
By the 100s AD, these bishops began to get together sometimes for councils, where they would try to decide how Christians should behave and what Christians should believe.
Soon these councils of bishops began to also choose new bishops when one died, and people didn’t choose their own bishops anymore. Instead of choosing any rich man, the councils chose men who were already priests (but usually men from rich families still).
In the early Middle Ages, some kings wanted to choose bishops themselves, instead of letting the councils do it. There were big fights between the councils and the kings. But by the later Middle Ages, it was pretty much the councils that won, and they chose the bishops for each town.