How does an ammeter in a circuit work?

Apologies for this simple question, but I'm having trouble grasping the concept of how an ammeter works.

Taking the following circuit as an example:

When the switch is closed, current flows through the circuit - ie. there is a potential difference between the two ends of the circuit, so electrons flow from the negative terminal to positive.
The ammeter records the current flowing through the variable resistor.

My question is: How can the ammeter tell how much current is flowing the resistor? since it's "behind" the resistor?
And also: Why and how does a resistor limit the current flowing through the entire circuit? doesn't it limit only the current that is flowing past and after the resistor?

Two questions:

How can the ammeter tell how much current is flowing the resistor? since it's "behind" the resistor?

There at least several means that current can be measured using different technologies. The early ammeters used galvanometric technology where a coil in the galvanometer becomes part of the current path. The coil generates a magnetic field and the magnetic field mechanically deflects in an angular fashion a permanent magnet attached to a dial pointer. But in today's technology we can sense the magnetic field using Hall sensors or more often we use a shunt resistor (low resistance resistor) that does not greatly impede the current however allows sufficient voltage drop to determine the current using Ohms Law.

Why and how does a resistor limit the current flowing through the entire circuit? doesn't it limit only the current that is flowing past and after the resistor?

First of all resistors 'limit' current by converting the electrical energy that flows through the resistor into heat energy. Secondly the current flowing into a resistor is equal to the current flowing out of the resistor. Although there is a voltage drop across the resistor, there is no 'current drop'. By dropping voltage across the limiting resistor in the circuit you lower the voltage drop across the rest of the circuit thus the current with the current limiting resistor in place is less through the entire circuit than if you did not have the resistor there. Another way to think of it is that by adding the resistor in series with the existing circuit, you have increased the total circuit impedance and by Ohm s law reduced current flow. $$I_{initial}=\frac{V}{R_{circ}}$$ $$I_{after}=\frac{V}{R_{circ}+R_{limiter}}$$

Why and how does a resistor limit the current flowing through the entire circuit? doesn't it limit only the current that is flowing past and after the resistor?

First, this is a DC circuit (ignoring the switch) which is to say that the circuit voltages and currents are constant with time.

Since that is the case, by conservation of electric charge, the current through the ammeter and resistor are identical for, if that were not the case, electric charge would necessarily accumulate somewhere between or within them and thus the voltages and current would not be constant in time.

In reality, Kirchhoff's current law (KCL) is only an approximation that is exact in the low-frequency limit. For frequencies high enough such that the physical extent of the circuit elements is significant compared to the wavelength of the EM waves at such frequencies, 'ordinary' circuit laws such as KCL do not hold.

How does an ammeter in a circuit work?

The most basic ammeter is illustrated in the attached picture. It is simply a wire inserted in an electric circuit with its + and - terminals (like a resistor) and below the wire there is a compass needle that rotates with an angle that depends on the electric current intensity, $I$, through the wire.

Source: Wikipedia, Hans Christian Ørsted