Ground/short circuit confusion
Adding on to Dave's answer,

An ideal op-amp follows two basic rules when operating under closed-loop feedback conditions:


The voltage at the two inputs are equal
There is no current flowing into or out of the op-amp inputs


Position 1:


In position 1, the voltage at the bottom input is Vin. Using rule 1, the voltage at node A is also Vin.

Since there's no voltage difference across the left resistor, there's no current flowing in. Using Kirchoff's current law,

\begin{equation}
\sum{I_A} = 0
\end{equation}

Since there is no current from Vin to node A and since the op-amp input can't source/sink any current, that must mean there is also no current flowing from node A to  Vout, thus Vout = VA = Vin. This means the gain is 1.

Position 2:


Here, the two op-amp inputs are set to 0V (due to rule 1). The current flowing from Vin to node A is thus:

\begin{equation}
I = \frac{Vin}{R}
\end{equation}

Again, the op-amp input sources/sinks no current so that means the current flowing from node A to Vout is:

\begin{equation}
I = \frac{0 - Vout}{R} = \frac{Vin}{R}
\end{equation}

Solving for Vout, we get
\begin{equation}
Vout = -Vin
\end{equation}

Thus getting the output gain of -1.