Most of the minerals in fertilizer are mined. The great exception is nitrogen fertilizer.
For nitrogen fertilizer, the process is as follows: 80% of the atmosphere that we breathe in is actually not oxygen but nitrogen gas, called dinitrogen. There is a synthetic chemical process, called the Haber-Bosch process, for which a Nobel Prize in chemistry was awarded around 1915. Briefly, the chemical bond in atmospheric nitrogen gas is broken down. It requires a lot of energy, and that energy comes from oil and natural gas. When you break that bond apart, you can then produce a compound such as ammonia. That's where the oil and natural gas comes in.