Dear student
While filling electrons in an atom's orbitals, we use Pauli's exclusion principle, Hund's rule of maximum multiplicity and Afbaus's principle.
Acc. to Afbaus's principle, in the ground state of atom electrons, must be filled in a lower state of energy first.
To compare energies of an orbital we use this formula = n+l
if this sum is less (comparatively) then that orbital has less energy compare to the other one.
So, For 3d
n+l = 3 +2 = 5
For, 4s
n+l = 4+0 = 4
hence, Energy of 4s orbital is less than 3d orbitals, because of this we fill 4s orbital prior to 3d
Regards