The easy rule of thumb is that when you see a key signature with multiple flats, the second to last flat is the major key. So with 4 flats you have: Bb Eb Ab and Db. The second to last here is Ab, so that’s the key.
When there’s only one flat, Bb, the key is F. You just have to know that.
A semitone is the same as a half-step, the difference between two consecutive notes of the chromatic scale. So to get from Ab to D, you have to count up the notes of the chromatic scale: Ab A Bb B C Db D. Counting the intervals, you get Ab to A is 1 semitone, A to Bb is another semitone, etc. By the time you get to D, you should have counted 6 semitones. So that means that D is 6 semitones higher than Ab. That’s why your transposition worked!
Of course, you can also count down: Ab G Gb F E Eb D. That give you 6 semitones lower from Ab to D – which happens to be the same number in this case, but won’t always be. This will also put you in D, but an octave lower than when you count up.
Also note that it doesn’t matter if you count chromatically by sharp or flat notes. For example, since Ab is the same as G#, you could count up: G# A A# B C C# D. You still get the same result of 6 semitones.
BTW, the rule for sharp keys is also easy. When you see multiple sharps, look at the last sharp, and raise it one semitone, and that’s your major key. So with 2 sharps you have: F# C#. Raise the C# a half step, and you get D. Voilà, that’s your key. With three sharps: F# C# G#, raise the G# a half step to A. And so on.
Note to other chiffers: Yes, enharmonic notes are the “same” only in 12TET tuning. Trying to keep things simple here. 
Edited to add: I see there were 3 other posts while I was writing this! I agree, the cycle of fifths is also good to know, once you’re comfortable with the scales and intervals.