In the earlier Middle Ages, towns arose from villages, and were, like the villages, part of the land holdings of feudal lords. Sometimes the holdings were divided or returned to the king and given to new lords, and it often came to be that towns were under the jurisdiction of a different lord than the surrounding countryside.
In the later Middle Ages when towns actually came into being they were run by a council made up of merchants and wealthy non noble families much like today. They would deal with taxes, protection, keeping the peace and making sure all the laws were obeyed and establishing those laws. The governments of these towns were republican in nature, and were referred to as medieval communes.
There is a link below to an article on medieval communes.