Why do phosphorus and sulfur have variable valences? I have learnt that phosphorus and sulfur have valences 3, 5 and 2, 4, 6 respectively because of vacant d orbitals. But how?
Phosphorus' configuration is $\mathrm{[Ne]3s^23p^3}$. There are 3 unpaired electrons that can get paired with bonds, so 3 makes sense (similar to nitrogen) and a similar argument for 2 with sulfur (similar to oxygen), but how do the vacant d orbitals help in giving valence 5?
Do 2 electrons from the s orbitals go to the d orbitals and then there are 5 unpaired electrons available for bonding and similarly for sulfur?
For a modern answer (with some hand-waving), we can simply say that valence electrons occupy more space as we go from first period to second period to third period. So hydrogen makes one bond, second period elements up to four, and third period elements up to six.
If you want to stick to Lewis structures and hybridization, you can explain $\ce{PCl5}$ with resonance:

Source: https://www.cusd80.com/cms/lib6/AZ01001175/Centricity/Domain/1108/003-center%204-electron%20bond.pdf
If you like molecular orbital diagrams, it is pretty straightforward to expand a $\ce{EX3}$ MO diagram to a $\ce{EX5}$ diagram without using d-orbitals, e.g. http://www.chembio.uoguelph.ca/educmat/chm364_preuss/5_5contd2%20MO%20Theory_ICl4%20PCl5.pdf. Like in the valence bond model, the bond order will decrease as you increase bound atoms.