Okay, lots of replies here.
But some clarification is needed. I talk about track circuits here, but they can be axle counter sections instead.
To keep things simple, the following refers to track circuits as if they are all single track circuit sections. In practice, they may actually be made up of more than one track circuit section, but the interlocking combines them and treats these groups as if they are a single track circuit section.
Hence there may be the following track circuits along a line in sequence: AA/1, AA/2, AA/3, AA/4. The interlocking combines the outputs from them to operate a 'repeat' relay, which may be called AA T2PR. It is this summation relay that the interlocking uses, hence the interlocking treats AA/1, AA/2, AA/3, AA/4 as one track circuit section.
Now, the signals...
First, I need to tell you that there are controlled signals and automatic signals.
A controlled signal normally will only clear when the signaller operates his/her control for it. If the interlocking determines that the section is clear of trains, the next signal is lit and any other requirements are met, the Interlocking will clear the signal to an appropriate aspect. Once a train has been detected as having passed the signal, it will go back to red and stay at red. It will not normally reclear without the signaller re-operating the control for it.
An automatic signal normally works automatically with no intervention from the signaller. They are normally used on plain line where there are no points or level crossings (that use signals for protection).
If the section ahead is detected clear of trains, the next signal is lit, and any other conditions are okay, the signal will clear to an appropriate proceed aspect. It will go back to red once a train passes it (* but see later) but once the section is clear again, it will automatically reclear.
For a normal controlled signal, there is a track circuit all the way up to the signal (often called the berth track). Then you have the signal. Then there is an overlap track circuit of between 50 yards and 300 yards. Normally an overlap is at least 200 yards. This is often called the overlap track. Then there is the next track circuit up to the next signal (the berth track for it).
It is the interlocking detecting the overlap track being occupied by a train that causes the signal to go back to red. Although note that any track circuit going occupied between the two signals will cause the signal to go to red.
Most automatic signals don't have a separate overlap track circuit. Instead the berth track circuit continues past the signal and ends at the end of the overlap (in other words at least 200 yards beyond the signal). It is the next track circuit that causes the signal to go back to red. Typically the berth track for the next signal. This does mean that if you are a passenger near the front of the train, you may see the signal still showing a proceed aspect out the window, well, until the front axle goes beyond the overlap and on to the next track circuit.
One of the other posters talked about last wheel replacement. These are not very common. They are controlled signals with special controls. Typically only found on platform lines at stations or depot lines or similar where shunting or propelling moves may be made. As they have said, the signal does not go back to red when a train goes past it. When it goes to red depends on the requirements at the location where each particular signal is located. It may also depend on the length of the train.