In school, this old boy taught himself musical composition by taking traditional compositions, decomposing them, and studying their components.
Here's biographer Christoph Wolff (Johann Sebastian Bach: The Learned Musician, 2000) describing Bach's process. "One of his study methods consisted of taking a given model and turning it into a new work, not by arranging it but by appropriating the thematic material, subjects, and countersubjects and rewriting the score to create a different piece—a new solution to what he took to be a musical question. And in the process of recomposing, he discovered new thematic connections or contrapuntal combinations as well as new harmonic, melodic, and rhythmic features" (p. 93).
Examples of exercises include a decomposition of Dutch composer Johann Adam Reincken's Hortus Musicus, which runs over an hour and is available HERE. Compare this to the fruits of Bach's study, the five-minute Fugue in B-Flat Major (BWV 954), his Sonata nach Reinken [sic] in A Minor (BWV 965/2), and his Sonata in C Major (BWV 966/2).
It stands to reason. Bach learned to compose by decomposing other music and studying how that was composed. Then he made his own early music from recombination.