We investigated how the magnetic field in solar active regions (ARs) controls flare activity, i.e., whether a confined or eruptive flare occurs. We analyzed 44 flares of GOES class M5.0 and larger that occurred during 2011--2015. We used 3D potential magnetic field models to study their location (using the flare distance from the flux-weighted AR center $d_{mathrm{FC}}$) and the strength of the magnetic field in the corona above (via decay index $n$ and flux ratio). We also present a first systematic study of the orientation of the coronal magnetic field, using the orientation $varphi$ of the flare-relevant polarity inversion line as a measure. We analyzed all quantities with respect to the size of the underlying dipole field, characterized by the distance between the opposite-polarity centers, $d_{mathrm{PC}}$. Flares originating from underneath the AR dipole $(d_{mathrm{FC}}/d_{mathrm{PC}}<0.5$) tend to be eruptive if launched from compact ARs ($d_{mathrm{PC}}leq60$ Mm) and confined if launched from extended ARs. Flares ejected from the periphery of ARs ($d_{mathrm{FC}}/d_{mathrm{PC}}>0.5$) are predominantly eruptive. In confined events the flare-relevant field adjusts its orientation quickly to that of the underlying dipole with height ($Deltavarphigtrsim40^circ$ until the apex of the dipole field), in contrast to eruptive events where it changes more slowly with height. The critical height for torus instability, $h_{mathrm{crit}}=h(n=1.5)$, discriminates best between confined ($h_{mathrm{crit}}gtrsim40$ Mm) and eruptive flares ($h_{mathrm{crit}}lesssim40$ Mm). It discriminates better than $Deltavarphi$, implying that the decay of the confining field plays a stronger role than its orientation at different heights.