In the ancient world, most mercenary forces were associated with a city-state or a tribe that held some territory of its own. For example, many Greek city-states 'hired out' their soldiers as mercenaries when not involved in their own wars. Sparta did this quite frequently, as they were renowned as some of the finest troops of the time. Athens did this too, which is why Xenophon's 10,000 had to make their famous march (recorded in his 'Anabasis'). Other ancient mercenaries were recruited from rural tribes, often on islands (slingers from Crete and the Balaeric Islands were famous ancient mercenaries, as were peltast-skirmishers from the Thracian and Isaurian tribes). When no longer needed by their employer, these mercenaries simply returned to their homelands.
In the medieval period, mercenary companies were very common during wartime. Often, like in the ancient era, they were hired out resident soldiers of cities (especially the northern Italian cities). Italian mercenary crossbow troops were famously employed by the French at the battle of Agincourt in 1415, for example. These troops would simply return to their homeland when released.
Another source of mercenary companies was the recruitment of volunteer knights and sergeants. A feudal lord would be able to call up the service of his retainers and vassals for a time, but any substantial invasion required additional troops. It was normal for a lord planning an invasion to put out the word that knights and sergeants who did not owe him fealty or service could join his army for pay or a promise of plunder. William the Conqueror's army that conquered England in 1066 consisted as much of volunteer mercenaries as soldiers who owed him duty. William's volunteer knights (who came from Normandy as well as what we now call Belgium) expected (and did receive) land grants in England if they won. Other volunteer mercenaries simply return to their own estates, towns, and villages with their share of the plunder.
During times of protracted warfare during the Middle Ages, some enterprising individuals created their own private armies specifically to be hired to the side that at any time offered them the richest contract. These mercenary armies could be small companies of only a hundred or so, but they also could grow into the thousands. Trained men could come from almost anywhere to join these businesses, and they would just return to their homelands with their pay when they retired. However, in the meantime, the standing force had to be provisioned in the land that hired them or the warzone. This meant that they would simply pillage and take whatever they needed, even kicking people out of their castles or homes if they could. There were times when rulers could not afford to pay mercenaries companies promised coin and instead gave them permission to sack and take wealth from particular towns as payment. Thus, it's no surprise that England's Magna Carta (1215) specifically banned the king from housing mercenary forces on English soil!
Medieval mercenaries company reached the largest and most destructive during the Hundred Years War (1337-1453) between England and France. Both slides liberally hired mercenary companies, which frequently pillaged French estates and towns (sometimes with and often without royal permission). George Martin's "A Game of Thrones" series is very much inspired by the Hundred Years War.