Why do 3d orbitals have lesser energy than 4s orbitals in transition metals?
Disclaimer: I now believe this answer to be fully incorrect.
  
  Please consider un-upvoting it and/or downvoting it. I do not like seeing incorrect answers at +22.
  
  However, I will leave it up for now. It is a reflection of what is taught in many undergraduate-level textbooks or courses. However, there have been criticisms of this particular graph in Shriver &amp; Atkins, as well as of the idea that the 3d orbitals are somehow higher in energy than the 4s orbitals. I believe it was mentioned that the energies were calculated with the outdated Thomas–Fermi–Dirac model, but cannot really remember. I will ask another question about the 3d vs 4s issue, but in the meantime I would point the reader in the direction of these articles: 
  
  
  Pilar, F. L. 4s is always above 3d! Or, how to tell the orbitals from the wavefunctions. J. Chem. Educ. 1978, 55 (1), 2 DOI: 10.1021/ed055p2.
  Melrose, M. P.; Scerri, E. R. Why the 4s Orbital Is Occupied before the 3d. J. Chem. Educ. 1996, 73 (6), 498 DOI: 10.1021/ed073p498.
  Vanquickenborne, L. G.; Pierloot, K.; Devoghel, D. Transition Metals and the Aufbau Principle. J. Chem. Educ. 1994, 71 (6), 469 DOI: 10.1021/ed071p469.
  Scerri, E. R. Transition metal configurations and limitations of the orbital approximation. J. Chem. Educ. 1989, 66 (6), 481 DOI: 10.1021/ed066p481.
  Some criticism of Atkins' books by Eric Scerri.
  




While Molly's answer does a good job of explaining why electrons preferentially occupy the 4s subshell over the 3d subshell (due to less inter-electron repulsion), it doesn't directly answer the question of why the order of the 3d/4s energies changes going from Ca to Sc. I stole this figure from Shriver &amp; Atkins 5th ed:



The red line represents the energy of the 3d orbital, and the blue line the energy of the 4s orbital. You can see that up to Ca, 3d > 4s but for Sc onwards, 4s &lt; 3d.



As chemguide rightly points out, up to Ca, the 4s orbital is lower in energy than the 3d. The energy of an electron in an orbital is given by
$$E = -hcR\left(\frac{Z_\text{eff}}{n}\right)^2$$
where $hcR$ is a collection of constants, $Z_\text{eff}$ is the effective nuclear charge experienced by the electron, and $n$ is the principal quantum number. Since $n = 4$ for the 4s orbital and $n = 3$ for the 3d orbital, one would initially expect the 3d orbital to be lower in energy (a more negative energy). However, the 4s orbital is more penetrating than the 3d orbital; this can be seen by comparing the radial distribution functions of the two orbitals, defined as $R(r)^2 r^2$ where $R(r)$ is the radial wavefunction obtained from the Schrodinger equation:



The 4s orbital has a small inner radial lobe (the blue bump at the left-hand side of the graph), which means that a 4s electron "tends to spend time" near the nucleus, causing it to experience the full nuclear charge to a greater extent. We say that the 4s electron penetrates the core electrons (i.e. 1s through 3p subshells) better. It is therefore shielded less than a 3d electron, which makes $Z_\text{eff}$ larger. Going from the 3d to the 4s orbital, the increase in $Z_\text{eff}$ wins ever so slightly over the increase in $n$, which makes the energy of the 4s orbital lower.



Now, going from Ca to Sc means that you are adding one more proton to the nucleus. This makes the nuclear charge larger and therefore both the 4s and 3d orbitals are stabilised (their energies decrease). The catch is that the energy of the 4s orbital decreases more slowly than that of the 3d orbital, because the 4s orbital is relatively radially diffuse (the maximum in the radial distribution function occurs at a larger value of $r$). If you have studied physics, you could think of it as the interaction between two point charges; if the distance between them is large, then increasing the magnitude of one point charge has a smaller effect on the potential energy $U = -\frac{kq_1q_2}{r}$. The faster decrease of the 3d energy also makes sense because if nuclear charge were to tend to infinity, shielding would become negligible; the orbital energies would then be entirely determined by $n$, and if this were to be the case, you would expect 3d &lt; 4s in terms of energies, as we said at the very start.

However, in Sc, the electrons preferentially occupy the 4s subshell even though it is higher in energy, and this is also because the 4s orbital is radially diffuse - the electrons have more "personal space" and experience less repulsion. One way of putting it is that an empty 4s orbital in Sc has a higher energy than an empty 3d orbital, but a filled 4s orbital has a lower energy than a filled 3d orbital. The fact that 4s > 3d in energy also explains why, for the transition metals, the 4s electrons are removed first upon ionisation ($\ce{Sc^+}: [\ce{Ar}](3\mathrm{d})^1(4\mathrm{s})^1$.)



I just want to end off with a comment that the factors that determine the electronic configurations of d-block and f-block elements are actually very closely balanced and just a small change in one factor can lead to a completely different electronic configuration. This is why Cr and Cu have an "anomalous" configuration that maximises exchange energy, whereas we don't get carbon adopting a $(1\mathrm{s})^2(2\mathrm{s})^1(2\mathrm{p})^3$ configuration in order to have "stable half-filled shells".