The Thirty Years War was ended by the Peace of Westphalia which was referred to as the "Peace of Exhaustion" by contemporaries. The Peace of Westphalia was not one specific treaty but rather a collection of treaties commonly linked by the fact that they brought the Thirty Years War to an end.
France and Sweden had already agreed at the Treaty of Hamburg that there should be a European return to the status quo of 1618.
Ferdinand III wanted to retain the gains made at Prague and he wanted 1627 to be his baseline on territorial negotiations.
The German Electors favoured 1618 as their baseline.
In September 1640, the Electors were summoned by Ferdinand III to Regensburg where the emperor attempted to get the Electors to agree to preserving the Peace of Prague. He failed. Frederick William of Brandenburg specifically rejected Prague as the basis of any settlement.
In July 1641, Brandenburg and Sweden signed a truce. Many German princes followed this example of Brandenburg's to show their displeasure with Ferdinand III. However, Ferdinand III had already started separate negotiations with the French and Dutch at Munster and with the Swedes at Osnabruck.
Peace negotiations continued at the same time as the military campaigns. In 1642, a Swedish army defeated an Imperial army at Breitenfeld at the same time as Swedish and Imperial diplomats were examining potential peace terms. Such occurrences happened as a show of strength to the opposition.
In 1645, the Imperial army faced two defeats at Nordlingen (defeated by the French) and Jankau (defeated by Sweden). The Holy Roman Empire was clearly in no position to carry on but neither could the Swedes or the French deliver a knockout blow from a military point of view.
In 1645, Sweden and Saxony signed a peace agreement.
In 1646, Ferdinand III could no longer expect support from Saxony, Brandenburg or Spain.
In 1647, Maximilian of Bavaria was forced by the Swedes and French to withdraw his support to Ferdinand. Maximilian reneged on this agreement in 1648, and Swedish and French forces devastated Bavaria leaving Maximilian in a position where he could not do anything else except sign a truce with Sweden and France.
The French persuaded Ferdinand III to exclude Spain from the peace negotiations but the United Provinces and Spain did sign a peace settlement at Munster in 1648 thus bringing to an end 80 years of hostility between the Spanish government and the Dutch commonly known as the Revolt of the Netherlands.
The whole package of settlements is known as the Peace of Westphalia. One of its provisos was that the practice of electing a King of the Romans in the emperor's lifetime was abolished. The title of the "Peace of Exhaustion" is probably a more apt title for this series of peace settlements that brought to an end the Thirty Years War.
France gained the bishoprics of Metz, Toul and Verdun; Breisach and Philippsburg; Alsace and part of Strasburg.
Sweden gained West Pomerania, Wismar, Stettin, Mecklenburg; the bishoprics of Verden and Bremen which gave her control over the estuaries of the Elbe and Weser.
Brandenburg gained East Pomerania; the archbishopric of Magdeburg and Halberstadt.
Bavaria kept the Upper Palatinate and the Electoral title that went with it. The Lower Palatinate was restored to Charles Louis, the son of Frederick and an 8th Elector's title was made for him.
Saxony kept Lusatia.
Bohemia remained an hereditary domain.
Upper Austria was restored to the Habsburgs - Bavaria had taken control of it.
Spain recognised the United Provinces as a sovereign state.
"The Peace of Westphalia". HistoryLearningSite.co.uk. 2006. Web.