In a secondary dominant such as "V of V", you think about the "of V" being a temporary I. For example if you go to Dm using a secondary II-V of Dm, then you do it as if in the key of Dm, for example Em7 and A7.
We say that we're "in" a key. But not "in a scale". "In D minor" means in the key of D minor.
Key is much more about a tonic than a scale. For example over the A7 chord, is B natural or flat? The A7 chord does not say anything about a B, and both alternatives work. The B question is left open by the A7 chord.
From your follow-up comment:
Isn't the V chord of the D minor scale (D, E, F, G, A, B♭, C) Am? (D,
E, F, G, A, B♭, C)
Am is a diatonic "v" chord in D minor, yes, but in order to be a proper dominant chord it has to be A major, or usually even A7. In most minor-key songs, the V chord is a major chord, meaning that a chromatic alteration is needed compared to the diatonic scale, the natural minor scale. When doing secondary chord progressions, there many chromatic alterations. For example in the key of Am, you might do B7 - E7 - Am. Or even "V of V of V": F#7 - B7 - E7 - Am. To have e.g. the B7 chord, D has to be made D# and F has to be made F# etc.
The same thing happens in major keys as well. The V of V in C would be D7, giving a V/V to V to I progression D7 - G7 - C. You need to make the F note sharp if you want to play a D7 chord. Or in C7 - F, using secondary dominant C7 going to F, thinking about F as being a temporary I chord. A "secondary II - V - I" in C going to F could be Gm7 - C7 - F. The C7 can be called "V of IV", abreviated as "V/IV". (Which can be a confusing notation, because in concrete chord symbol names, the slash is used to denote a bass inversion, not secondary chord role.) To have this harmony progression, the B note has to be made temporarily flat. As soon as you get to the target F major chord, you can return the B note back to B natural.
It seems to be a very common mistake to assume that music has to be "in a scale". These people have great trouble understanding even basic polka songs in minor keys, and they try to come up with theories like "it's in harmonic minor" or "it's in melodic minor", just for the sake of constructing a single seven-note scale where all the chords can be fitted without needing chromatic alterations.
More additions from your comments:
Correct me if I am wrong: I am under the impression that chords are --
at any given time -- played in a context (a scale); that is not to say
there only is one "true context" at any given time, but there is
always at least one. When composing, you have to have an idea of what
specific context you're in, so that you can on the basis of that know
what chords goes well with the context you have in mind.
I think I agree with that, but I'll elaborate.
Thinking that a scale is something concrete, a law of nature, is a common assumption that causes problems understanding actual music. In reality, sounding chords wipe the floor with scales. Play a Dm and some "normal" Dm melody stuff over it. Then play an Eb major chord. What happened? The chord bulldozed your scale's E note and forced it flat, no permission asked. Now an E natural melody note would sound wrong! But then play a Dm9 chord - the chord's E note kicked your Eb idea away and back to E natural. Chords rule, scales follow. Or more like - whatever you actually play, affects your scale assumptions and the so-called harmonic context in your mind.
When composing or improvising, it's quite essential to have some kind of an intellectually definable idea of the harmonic context, and it can be explicated in terms of a scale. But even then the scale is just a reference grid that helps you reason about where things are. It's like a ruler that you might place on a drawing. It's not a rule about what can or cannot be done. Decide what chords you want, and then you can think about what the chords did to scale possibilities. Sometimes you find new chord combinations that do something interesting and you have to poke around to find where scale degrees could be, i.e. what harmonic contexts you could superimpose over the new situation. But you might also simply accept the chords as-is without explicating a complete scale over them. One example that comes to mind is "planing" chromatically, for example in Dm, if you descend chromatically like Am7 - Abm7 - Gm7, what scale might there be over the Abm7? Does it matter? Many people are able to use chromatic tricks like that completely fine without caring about the scale question.
As an exercise, play the following chords: C, F, G7, C, Gm7, C7, G/F, C/E, F/Eb, Bb/D, Eb/Db, Ab/C, Dm7, G7, Ab/Bb, C. What happens to the harmonic context along the way? Did the tonic really change - you did remember the initial C even after all the changes happening in the middle?
All that said, it is possible to think either way. Decide on a scale and use whatever chords it allows - like in modal music. Or think about chord progressions you want to have, and let the scales follow. Both approaches are used, but IMO, the chords-first style is more "melodic".
The difference in perspective extends to even how you view a scale or a mode. Is the Dorian mode a minor mode where the sixth degree has been raised, or is it a minor mode where you have a IV major triad instead of minor? Both aspects are true of course.