They work on static and dynamic boats. You need a boat with stern hydroplanes for them to work, they don't work well with front hydroplanes.
Submarines will, in general, not remain straight and level when travelling underwater, dynamic forces acting on the hull tend to tilt it upwards or downwards, and these problems get worse as the boat travels faster.
The operator of the boat will try and compensate for this, but generally we do a poor job and tend to over control the boat, leading to the boat porpoising.
Submarines with hulls optimised for operation on the surface tend to be worse than more streamlined designs in this respect. However the latter tend to travel faster which offsets that advantage.
A pitch controller adjusts the rear planes automatically to help keep the boat stable underwater. Earlier designs were based on pendulums, and you'll still see some of these in action. They tended to be a little bulky, and were time consuming to produce (hence expensive) so weren't at all well suited to smaller models. That all changed with the advent of accelerometer based pitch controllers, essentially a pendulum mechanism on a microchip.
How do they work, well think of our pendulum. With the boat travelling straight and level, the pendulum is undisturbed. If it starts to take a dive to the bottom, the pendulum will move as it's referenced to gravity. Sensors detect this and via circuitry a signal is sent to the servo controlling the rear hydroplanes to move them upwards by a certain amount and hence bring the boat level again. If the boat then begins to to travel to the surface the pendulum will swing the other way, and the opposite will happen.
This cycle constantly repeats itself, so you can appreciate the servo controlling the rear hydroplanes is kept as busy as a bee.