but we don't teach this because you get given the periodic table)
However, when we REMOVE electrons, we take them from the highest principle quantum number orbital, and this leads to the funny result that for the transition metals, we add electrons to the (n-1)d orbitals but remove electrons first from the ns orbital. If you're happy with this rule, stop reading now. Apply it and you will be fine!
If you're not happy with the rule, good. You're thinking critically, and this rule doesn't really make sense. Surely if the 4s orbital is lower in energy, we should remove electrons from it before the 3d orbital?
Well, in actual fact, it's NOT lower in energy for the transition metals. The increase in Zeff as we move to the right draws in the 3d orbitals more than the more expanded 4s orbital, and it ends being higher in energy:
So what actually happens as we move across the d block is that we add electrons to the 3d orbital first, because it's lower in energy (though it wasn't for K and Ca). So why do we end up with 4s electrons at all? It turns out the energies of the 4s and 3d orbitals are close enough in energy that electron-electron repulsion becomes important. So for Ti for example, the first 2 electrons go into the 3d orbital. The next 2 however can reduce their e-e repulsion significantly by going into the slightly-higher-in-energy-but-much-larger 4s orbital, and they do so. For Cr, the first 5 go into the 3d orbitals, but only one goes into the 4s orbital.
Still confused? For the full story, see http://pubs.acs.org/doi/pdf/10.1021/ed8001286.