Electrons are filled according to the energy of orbitals, lowest filled first. (Aufbau's rule) When it is unfilled, 4s orbital has lower energy than 3d, that is why 4s is filled first followed by 3d. To explain why 4s has lower energy than 3d, it has to do with the shape of the orbitals, which i'm not very familiar with to explain it to you.