also, you don't want a heavy accent at the end of a phrase - so in the fuga V (measure 3) my older french prof put in 2nd finger of rh on the c#. the preceeding f# is 4, e - 3, and d - 1 (which leads to cross over of 2nd finger to the c#).
it is also that way in the beginning, with the lh. some people try to extend their fingering (reaching way up and down - and don't settle for thinking about finger strengths). in the first measure when the notes start going down on b - he wrote 1 - 1- 2-3 (next measure) 4 (on f#). using the same idea of fingering for this four note phrase, he started the next one by lifting off and placing the thumb again on g. (g-1, f#-2, e-3, d-4) and again (e-1 and a-5 - then 1-3-2-1 on the crossover up to a).
hope this adds to the ideas of 'why a fingering is used?' finger substitution often happens in bach as well. you hold a certain note down and change fingers while it is held to reach another note. without this little trick, it would be hard to reach some of the notes.
also, some repeated notes (such as measure 20) need fingering (ie. from 5 to 4 to 3 on the d) which sets you up for a trill after using the 4th finger on e. the trill would be d-c-d-c-d-c with 323232)
and, as a ps, you don't always follow scale patterns for fingering in bach. actually, he's quite eccentric about his fingerings. you can see, as an example, the third measure in from the end (g-f#-e-d- cnatural-b- a-g-f#-e-d would be 2-3-1-2-3-4-1-2-3-4-5) is not played as a D scale pattern. much better this way.