Rules for filling up atomic orbitals can be understood by considering the governing principles:
Aufbau principle: electrons fill from lowest energy level to highest (attraction of positive nucleus on electrons: the farther they are from nucleus, higher is their energy or shallower potential well)
Pauli exclusion principle: no two electrons can have all quantum numbers the same; in particular, only two electrons with opposite spins can occupy an orbital. Pairing up of electrons with opposite spin raises its energy. Electrons with parallel spins have a repulsive exchange interaction between themselves.
Hund's rule: electrons are stabilized if they have parallel spins spatially distributed among different orbitals of a subshell. They'll get paired up only after each subshell has one electron, all having parallel spins.
Formation of half filled or fully filled orbitals thus has a stabilizing effect. Energy difference between $n$s and $(n-1)$d is not so high. In case of some special elements, transition of an outer electron from $n$s to $(n-1)$d is energetically favorable if it effects net stabilization due to maximization of electrons with parallel spins in d-shell, thereby over-riding the aufbau principle.
Nb, Mo, Pd, and Ag are some examples of special configurations. These principles should hold true for ions as well if we consider the remaining valence electrons in the system.