I've tried this delay function but it's a bit tricky because as long as the proximity sensor is still reading LOW, it just keeps looping and the motor never stops, causing it to bear down super tight and try to break itself.
Try this statement again WITHOUT using "it". We have no idea what "it" refers to.
const int proxyPin = 40;
const int proxyPin2 = 41;
So, you have two proximity sensors numbered uh-huh and two. Why? One and two make more sense.
digitalWrite(pwm, LOW); //***NOT WORKING***
It IS working. The next iteration of loop() will find that the proximity sensor is low, because the gate is closed, so it will try to run the motor some more.
You need a variable to keep track of the state of the gate - open, opening, closed, closing, etc. When the gate is closing, and the proximity sensor becomes low, run the motor for one second longer, then shut it off and set the state to closed.
If the gate is closed, don't try to close it more.