The electron configuration describes the most likely arrangement of electrons in the electron cloud. The quantum mechanical model involves electron shells, indicated by the principle quantum number which is indicated by the coefficient in the electron configuration of the atom. The quantum mechanical model indicates that each shell is subdivided into orbitals, which describe the shape of the most likely location around a fixed point where electrons could be found. These are indicated by the letters, s, p, d and f which each represent a particular shape. There are two additional quantum numbers in which to describe an electron's location, however these are not necessary to answer your question.
The orbital filling sequence is based on the lowest potential energy of electrons in each orbital. Generally, the lower the principle quantum number, the lower the potential energy of the electron in such level. However this is not always the case. Once the atom has more electrons than 18, the next electron is in the 4s orbital rather than the 3d due to the fact that the energy of an electron in a 4s orbital is just slightly less than an electron in the 3d orbital. The energies of successive orbitals are much closer together which muddies the orbital filling sequence for students.
So, how is a student to know the order? There are two ways:
1) use the orbital filling diagram shown below (picture #1). The drawback to using this diagram is that a student is dependent on the diagram to write all electron configurations.
2) (this is my personal favorite) use the periodic table shown below. (Picture #2) If a student needs to write the electron configuration for any element, he or she may start at the beginning of the table, with atomic #1, and fill the 1s orbital completely. Since an s orbital can hold up to 2 electrons, the next orbital to be filled would be the same one as Lithium, atomic #3 element, which is the 2s. This orbital also holds a maximum of 2 electrons, totaling 4. So the next orbital to be filled would be the same one as boron, which is atomic number 5. a p orbital can hold a maximum of 6 electrons, totaling 10 electrons. Continuing in the manner, after 18 electrons are placed (1s^2 2s^2 2p^6 3s^2 3p^6), the next orbital to electrons are placed into the 3s, then then 3p. Next to be filled is the same as potassium, which is atomic #19, the 4s. After this one, the next electron would be placed in the 3d (see photo#2).
Following the periodic table, the complete order is:
1s 2s 2p 3s 3p 4s 3d 4p 5s 4d 5p 6s 4f 5d 6p 7s 5f 6d 7p.
In the near future even larger elements may be formed. If element 119 were to be synthesized, where do you think the 119th electron would go?
It would go into the 8s orbital!
For filling up the electrons in the orbitals, there are basically 3 rules to be followed and they are :-
1) Afbau's Principle:- It states that " Filling of the electrons takes place from lower energy subshell to higher energy subshell".
The energy of the subshell is decided by the (n+l) rule; where n = Principal Quantum Number & l = Azimuthal Quantum Number
Greater the value of (n+l); higher will be the energy of the subshell.If the value of 2 subshell is the same ; for example 2p & 3s subshell has both (n+l) value = 3, in that case the subshell with higher 'n' value will have the greater energy.
Now, coming to your question:-
4s subshell has (n+l) value = 4 while 3d subshell has (n+l) value = 5. Thus, 3d subshell is filled after 4s subshell.
The other two rules governing the electronic configuration are:-
2) Hund's Rule of Maximum Multiplicity
3) Pauli's Exclusion Principle