Some reasonable answers so far but all have missed the key point - the speed of sound.
Pressure waves travel around in a cylinder at the speed of sound. This is a finite quantity and for the purpose of understanding the cycles in a 4 stroke engine, for each given condition doesn’t vary much.
During the intake stroke a low pressure wave travels upwards from the piston crown to the cylinder head. When the piston reaches BDC (bottom dead centre) the effect its change of direction will have on the incoming mixture starts to travel upwards at the speed of sound. That is, as far as the incoming mixture at the inlet valve is concerned, the piston travel has not yet changed direction because the effect of that change is still on its way up. At this point, it’s best to leave the inlet valve open until the pressure wave reaches it.
Now, consider the effect if you retain this same valve timing, but halve the speed of the engine. The pressure wave travels upwards at the same speed, but the inlet valve remains open for twice the time after BDC, due to the halved engine speed. The pressure wave reaches an open inlet valve and pushes mixture out again until it closes, resulting in reduced volumetric efficiency and loss of power.
It should now be obvious that the timing of inlet valve closing can be idealised for any specific RPM point at the expense of all others, and this is the simplest of the valve timing points to understand in terms of volumetric efficiency. All the valve timings affect RPM specific efficiency in some way, and all are related to the dynamics of fluid motion.
A further significant factor is the effect of manifold runner length on the overlap period, ie. the period around TDC (top dead centre) where the exhaust and inlet valves are both open. When the inlet valve closes a pressure wave starts to travel at the speed of sound back along the intake manifold runner due to the sudden stop of flow, reflects at the other end of the runner and travels back towards the valve. If the time taken for this reflected wave corresponds with the next opening of the valve, it will force mixture into the cylinder against the pressure of the exhausting burnt gases, resulting in more complete removal of these gases from the next cycle. Again this process is RPM dependent, manifold runner lengths can be tuned to optimise a specific RPM range due to the speed of sound in the runners.
As eluded to in other answers there is also the effect of inertia of the intake gas. This is tricky to get right and is dependent on smooth transitions all the way along the intake runner, through the port and finally past the valve. There’s no point finding an ideal intake runner cross sectional area, only to have that fluid momentum lost to an overly large intake port. Again, there is an ideal flow rate that is only achieved over a narrow RPM range, so it’s possible for a manifold to be too big for best performance at any specific engine speed.
Engineers can use these and many other factors to determine the torque curve of an engine. High performance engines will aim to have all design factors reaching ideal efficiency at a similar high RPM point. An engine designed this way would be referred to as “peaky”, and require a close ratio gearbox to keep it within its designed narrow power band. An engine with a “fat” torque curve might have reserved valve timing for good low RPM performance, but manifold tuning that aids high end power.
Of course most modern engine designs get the benefit of all sorts of dynamically adjustable timings and geometries. This really started with ignition advance systems dating back forever, through variable intake systems, variable valve timing and lift of various flavours, and even variable compression. These designs have mostly eliminated the issues that challenged tuners of yesteryear, making more power in a wider band, less pollution and better economy than they would have ever thought possible.