In the beginning, arguably the three most important things to remember about an ideal opamp operated without feedback is that it exhibits infinite input-to-output gain, that if the voltage on its non-inverting (+) input goes more positive than the voltage on its non-inverting (-) input, its output will be forced to as close to the positive rail as it can be, and that if the voltage on its non-inverting input goes less positive than the voltage on its inverting input, its output will be forced to as close to the negative rail as it can be.
Operated using feedback and implicit in the operation of the opamp - but perhaps not immediately obvious from the foregoing - is the fact that the opamp's output will do whatever it has to to make the voltages on the opamp's inverting and non-inverting inputs equal.
THE NON-INVERTING VOLTAGE FOLLOWER:
Referring to Figure 1, following, U1 is wired as a non-inverting follower with, say, zero volts on Vin, which is the non-inverting (+) input of U1.
In operation, then, if Vin (U1+) at zero volts was more positive than U1-, Vout would go more positive and, being connected to U1-, would force U1- closer and closer to the positive rail until Vin, Vout, and U1- were all equal to zero volts.
If the opposite were true and Vin at zero volts was less positive than Vout, then Vout (and U1-) would be forced closer and closer to the negative rail until the voltages on Vin, Vout,and U1- were all equal to zero volts.
Notice that - in any case - Vout will be forced to servo toward the voltage on U1+, whatever that voltage may be, which is why this circuit configuration is called a "voltage follower" and always has a gain of 1.
THE NON-INVERTING AMPLIFIER:
Referring to Figure 2, below, U1 is wired as a non-inverting amplifier with one volt on Vin, which is the non-inverting (+) input of U1. The gain of the circuit is dependent on the ratio of the resistors in the voltage divider R1 R2, and is equal to:
$$\ Av= 1+\frac{R1}{R2} $$
In operation and - as noted earlier - since the goal of the opamps's output is to do whatever's necessary to make the voltage on U1- equal to the voltage on U1+ and, since the voltage divider is functioning as an attenuator, U1's output amplitude must be equal to the voltage appearing on U1+ times the ratio of R1 to R2 to make that happen.
THE INVERTING AMPLIFIER
Finally referring to your question: "Why is the voltage gain \$ -\frac{Rf}{Ri}\$ " and to Figure 3, below, U1 is wired as an inverting amplifier with one volt on Vin, which is connected through R1 to the inverting (-) input of U1. The gain of the circuit is dependent on the ratio of the resistors in the voltage divider R1 R2, and is equal to:
$$\ Av= - \frac{R2}{R1} $$
In operation, with Vin being positive and U1+ being connected to zero volts, Vout must go negative in order to make the voltage on U1- equal to the voltage on U1+ and, consequently, Vout is an inverted version of Vin.