How derive g and u symmetry labels for orbitals?
g and u labels are only used when the molecule itself possesses the inversion symmetry element. For a diatomic molecule, this means that both atoms have to be the same, i.e. it has to be a homonuclear diatomic molecule $(D_{\infty\mathrm{h}})$.


  How do I carry out the inversion?


$\hspace{25 mm}$


Let's consider the $\ce{O2}$ molecule.
Let's consider the $\pi$ bonding MO. (Or one of them, to be precise, since they occur in degenerate pairs.)
Find the point of inversion of the molecule. Clearly it has to be the midpoint of the O=O bond, i.e. exactly in the middle.
Draw a straight line from one lobe to the point of inversion.
Extend the straight line through the point of inversion. You will see that you have gone from a shaded lobe to an unshaded lobe, indicating that the phase has changed. Therefore, the $\pi$ bonding MO is ungerade, or u.


If you start from the bottom-left lobe, you would draw a line through the point of inversion and end up in the top-right lobe, and you would draw the same conclusion.


  And what when the two overlapping orbitals are not of the same type, e.g. a $\pi$ overlap between d and p orbitals? Will the MO ever have g symmetry?


The parity of such MOs cannot be classified as g or u. However, if we are talking about a homonuclear diatomic, then this wavefunction is not acceptable. You cannot have a wavefunction that has only d orbital character from one atom, but only p orbital character from the other. By symmetry, the contribution from both atom's d orbitals have to be equal.

This arises because the Hamiltonian is invariant to the permutation of identical particles. Essentially, the idea is that both atoms are exactly identical, so there cannot be any way for them to be distinguished from each other. If you had such a wavefunction, and if it was populated, then there would be different amounts of electron density on the two atoms, which would mean that the atoms could then be distinguished. This is quantum-mechanically forbidden.

Such MOs may arise in heteronuclear diatomics. However, heteronuclear diatomics $(C_{\infty\mathrm{v}})$ do not possess the inversion symmetry element, and accordingly their MOs cannot be classified as g or u. Since inversion does not preserve the molecule, there is no requirement that it should preserve the wavefunction.


  What if one atom does not have any d orbitals?


There is no such thing. Every atom has d orbitals.

Whether they have the same energy, or whether they are occupied, is another thing altogether. If the energies are different, or if one atom has occupied d orbitals and the other does not, then it is a heteronuclear diatomic and as I said earlier, the g and u labels are not appropriate in such a case.