It is pretty simple, just a little logic. Look at the circuit for the inverting amplifier, put some round figures in for the input resistor and the feedback resistor, say 10k and 40k respectively. The formula then says the gain of the amplifier is -4. So lets imagine we put a signal of -1 volt on the input to the circuit. The amplifier itself has very high gain so the output immediately starts shooting up towards the positive rail... now look at what happens to the negative input to the amplifier... it is connected to a voltage divider consisting of a 10k and a 40k resistor between the amplifier output and the input signal so the voltage at the input will be one fifth of the voltage between the output and the input and when the output passes through +4 volt the potential at the negative input terminal of the amplifier will be the same as that to the positive input, ie. 0 volts and the output will stop rising... the gain will be -4 as predicted by the formula. Any deviation from this state of affairs is immediately corrected.
The same reasoning can be applied to the non-inverting amplifier circuit but in this case the feedback ratio is determined by the resistor pair connected to the output and the correct formula for the gain is easily derived. I hope this helps the formula make sense.