Baroque vs. classical music: That is at best a very simplistic view of the situation: see
http://en.wikipedia.org/wiki/Baroque_music#Transition_to_the... . Also, note that counterpoint remained alive and well, though it certainly did not have the importance it had in the Baroque period, through the Romanticism and the 20th century. The chorale (second movement) from Vierne's second symphony, Shostakovich's cycle of 24 preludes and fugues, and the canon section from the first section of his 7th symphony come to mind.
Furthermore, my impression (though I cannot find a citation to back this up) was that many if not most of Bach's compositions were produced by taking elements of popular culture and trying to produce from them something people would find meaningful on the next Sunday. Consider the Passion Chorale, which Bach used in (among other things) the Christmas Cantata and the St. Matthew Passion. The tune was originally composed 50 or 100 years earlier by Hans Leo Hassler for a song whose title was something like "my heart is beguiled by a pretty maid"; the most common words ("O Haupt voll Blut und Wunden") are from a German translation by Paul Gerhardt of a mediaeval Latin hymn.
All of this is not to deny or even to downplay Bach's genius. He simply, as derefr says, took popular inputs and did amazing, wonderful things.