KSW. The main reason that your last train still leaves the city around 1830 is staffing issues. The signal cabins on the route operate on 2 shifts and are open from 0430 till 2130. A train leaving town around 2030 would only reach Wicklow around the time the signal cabin is due to close.
Regarding trains staying over night in Longford. There is a driver depot based there. All Gorey/Enniscorty commuter trains are crewed by Connolly drivers which is the reason they return to Connolly at night.