At-grade ("Level") crossings should be closed (and thus grade separated). Where that's impractical there are numerous options for ensuring the crossing is not open to railway traffic until it is clear:
In some areas it can be practical to employ a person whose duties include verifying that this crossing is clear and then authorising its use. Because CCTV is cheap now we can fit cameras and lighting and have them do this remotely for a large portion of the network.
Or we can use radar to protect crossings. The barriers drop, and then radar is activated (the small hazard introduced by using radar is mitigated because the crossing is closed, nobody should be in the area being illuminated by the radar anyway), if somehow there is a large object on the crossing even though the barriers are down, this means something went wrong. The outward flow barriers re-open to potentially allow a trapped driver or animal to leave and the system tries again, if this doesn't help the crossing is considered failed and humans go to investigate, they will find your broken down semi-truck.
The freight car should preferably be harmlessly derailed by trap points. If this is impossible (and the layout cannot be rectified) the presence of the freight car should trip sensors, either axle counters or track circuits, and close the section to traffic. Again humans will likely have to come investigate how that occurred when it cannot be explained and won't go away.
At the crossing are boards indicating that this is a railway crossing, two sets of lights for each direction of traffic (two amber, one red in each set) and barriers, typically long motorised boom arms from which a flimsier but more visible structure hangs, plus a tall post with CCTV cameras mounted on it. The CCTV feed goes with all the other signal data to either a "signal box" not so far away, or a larger centre where many signallers work together. A signaller who wishes to move a train through the crossing views the CCTV feed, and initiates a sequence in which the lights show alternating amber ("caution, stop if possible"), then red ("stop"), the barriers close road lanes entering the crossing, then barriers close the road lanes which exit the crossing. The signaller verifies everything is as intended (e.g. no vehicle stranded on the crossing, no idiot pedestrians clambering over the barrier and standing on the railway) and then authorises trains to pass. Once as many trains as necessary have passed, the signaller closes the crossing to trains, and tells the crossing to re-open to road traffic, it raises the barriers and then switches off the red "stop" lights and road users can now use it as normal.
I'd take Amtrak to my hometown, and the engine would blow past the platform at 20+mph and still stop the passenger cars right on the platform. Riding on the train and it seemed like they'd miss it.
I think the weight of a car itself wouldn't change the theoretically possible stopping distance, since the momentum is proportional to the mass, but so is the friction between wheel and track, so mass cancels out.
For an open system, you have to be able to respond to arbitrary visual input. For a closed system, there shouldn't be random stuff on the right of way.