There are 2 systems that railways use, if its not one its the other, the first is a shunting system where if a train is nearing a railway crossing at grade the system sends out a electrical signal along the rail to see how far and how slow or fast the train is moving to set off the appropriate time to activate the crossing, the other such system is an oldie and is still being used today, and that is the use of insulated joints that keep the polarity poles and the electrical contacts different on each section of track for example, there are 2 sections of track, track A has the south rail negative and the north rail positive, when it comes to track B the polarity reverses so now the south rail is positive and the north rail is negative and this goes back and forth along the mainline. But anyways when a train passes over a insulated joint the relay's in the signal cabinet short out causing the crossing to activate due to that the train created a electrical connection on that insulated joint, when the end train has passed over the 2nd last insulated joint after the crossing, the relays in the signal cabinet reset and the crossing has stopped activating, but also the polarities changed when that train passed over that section of track too. Also if a train activates the crossing and stops inbetween the first set of joints they went over and the second that they didn't reach then the crossing stops activating and the gates go back up the max time for the delay is 20 seconds. But there are instances where if a train is stopped to close to the crossing due to that they made a lift at a siding somewhere and they had to block the crossing, the crossing stays active no matter what until the train has fully cleared the second set of insulated joints in order for the crossing to reset itself.