You have unfortunately & unwittingly answered your own question wrongly and it is precisely this that is causing you the confusion:
"I know [an] answer is normally in [the] dominant key."
It is not. Counterpoint is a linear game (that can and does result in vertical (harmonic) moments that can and may be functionally explained in terms of harmony) and the terms 'subject' and 'answer' must be defined in a relationship that demonstrates their voice leading. The 'answer' is defined by the intervallic relationship the composer has set for its first entry in relationship to the 'subject': at the fourth, at the fifth, at the [whatever]. Bach obligingly almost always sets that relationship at the fifth (above) and fourth (below) in WK I & II, which makes subsequent harmonic explanations easier, as this often coincides with the dominant harmonic function, which is where your confusion stems from.
If I take your Fugue in E-major from BkII (BWV 878), the exposition has come to settle in the dominant, B major, in bar 19 (we have a full implied cadence in that key), and the bass enters (on B) with what therefore is the subject. As is to be expected at this point, Bach at once embarks upon a stretto, and the tenor enters prematurely at the fifth (F#) in relation to the bass's (B), and therefore provides the answer-in-stretto.
Passing over a treatment of countersubject material & free CP that follows, in bars 26/7, Bach starts a fresh stretto, now in diminution, and in tonally speaking a much more ambiguous context. The soprano starts the party on G# (bar 26) at a moment where we are about to cadence to I(64) in C# minor (perfectly reasonable: relative minor to the main key we're trying to reach again), but at the moment of entry no key is stable enough (yet) to make the 'dominant/tonic' decision sensibly. In purely linear terms, however, it's not so hard at all, for the alto (continuing on its way from previous efforts) slips into the stretto on D#, at the fourth from G#, and is therefore an answer to the soprano's subject. Meanwhile, the other voices going about their business hasten to a full (but very temporary) close in B major (bar 28) where (although harmonically undone by the alto's A natural immediately preceding above) the subject enters in the tenor (still in stretto in diminution) on B, immediately answered at the fourth by the bass and, not to spoil the fun, one more time (still in diminution, bar 30), now at the fifth below its own preceding entry on F#, i.e. at the octave on B, as the last instance of the stretto subject in diminution while, majestically but ever so slyly, the alto heralds the full return (on E, bars 30-32) of the original subject unchanged, immediately above the (bass) stretto subject's very last moments.
In harmonic terms, at the point of the alto's subject entry on E in bar 30, we're nowhere near the home key yet -- we're still seemingly trying to get away from the relative minor (and VI in E: that's the sleight of hand Bach is relying on here) 'on the way home' instead -- but in contrapuntal terms this fugue is well and truly back where it started, fully restating the subject on E, unchanged. Job done -- from Bach's point of view. :-)
In determining entries in stretti and other developmental episodes, the only stab you will ever get at identifying subjects and answers, is by arguing it linearly according to intervallic relationships, helped where they can do by any apparent underlying implied or explicit harmonic events. Reverse the importance of those considerations, and you will end up in a morass of harmonic ambiguity not of the fugue writer's making, and that really ain't a pleasant place to be... :-/
Edit:
Yes, profoundly wrong: to treat all voice entries equally as subject entries denies the whole point of the 'drama' of contrapuntal writing as well as the essential principle of voice identity and voice differentiation through voice leading and vigorous part wrting. To suggest, by such a terminology, that a subject entry at the prime and an answer entry at e.g. the fifth are identical, is to deny the entire principle of (fugal) counterpoint altogether, as well as the practical evidence of one's ears. It's as wrong-headed as it is rooted in vertical (harmonic) practice & thinking, where a subject presented in whatever key remains the subject regardless. That is absolutely not the case in counterpoint: the very dynamism and intricacy of the discipline depends on that not being so.
In like manner, identifying the central section of strenuous contrapuntal development and intensification of voice entries as 'modulatory' is equally wrong-headed in that it completely sets the cart before the horse. Comparatively speaking, any modulation is the by-product of the greatly intensified contrapuntal activity at that point, to exhaust the vocal and contrapuntal possibilities of the subject(s) to the limit so that the need to return to a restatement becomes undeniable in contrapuntal terms. To place modulation central would condemn the counterpoint to the role of a vamping-till-ready bit-player in its own fugal theatre, filling space and time while the modulatory ground plan unfolds all around it regardless. That's not the art of fugue, not to mention that it'd be terrible counterpoint.
Hope this helps. All the best,