Surprisingly, the tradition of lager brewing grew out of a long period of bad beer in Bavaria, particularly in the summer months. Warmer temperatures promoted the growth of a host of different spoilage organisms that were largely dormant in the brews of the colder months of the year. Summer beers were often so bad that brewers would resort to using a number of products to mask the flavor including oxen bile, chicken blood, soot, tree bark, or even poisonous mushrooms (it really is a testament to how bad the beer was that oxen bile was preferable to the unadulterated beer). Of course, this was long before the discovery by Louis Pasture in the late 1800's that living organisms caused beer spoilage, so the powers that were did what they could to try and mitigate the issue.
Over the course of a few centuries, there were multiple attempts to regulate the quality of beer. In 1156, the city of Augsburg issued the first decree on spoiled beer insisting that all the city's bad beer "shall be destroyed or distributed amongst the poor at no charge." This held citizens off for another two hundred years when in 1363 twelve members of the Munich city counsel were appointed to inspect the quality of the city's beer. Further, in 1420 Munich decreed that beer had to be aged for a minimum of eight days. In 1447, the precursor to the famous Reinheitsgebot was laid out saying that Bavarian beer would only consist of barley, water and hops (remember, the existence of yeast in beer was not yet discovered). And finally, in 1516 the Bavarian Duke Wilhelm IV extended the 1447 decree to cover his entire kingdom.
Those attempts at quality were all well and good, but they didn't have the intended effect, and summer beer often continued to be putrid. Although far less famous than the Reinheitsgebot, the legislation laid out by the Bavarian Duke Albrecht V in 1553 had far more significant ramifications for the beer world. Duke Albrecht V simply forbade the production of beer altogether between the Feast of Saint George (April 23) and Michaelmas (September 29). This did succeed in stemming the production of bad beer but, more importantly, it unintentionally led to the development of lager brewing. As production was halted in the warmer months of the year, ale yeasts were phased out and only the yeasts capable of fermenting at colder temperatures survived, eventually hybridizing to create a new species of yeast. Stronger beers brewed towards the end of the brewing months became known as March beers or Märzenbiers in German and were stored in cellars or caves for consumption until brewing could recommence. The actual term "lager" is derived from this practice and comes from the German lagern, which means, "to lay" or "to store".
Duke Albrecht's prohibition on brewing during the summer was finally rescinded in 1850. By that time lager brewing had taken over in Germany and was beginning to spread to other parts of the globe, most notably Bohemia with the birth of Pilsner by Josef Groll in 1842. Of course, lagers are not the only type of traditional beers brewed in Germany. Weissbier and hefeweizen are brewed using top fermented yeast at warm temperatures and their history is quite interesting (although I will save that for a later time so this doesn't turn preposterously long).