The gap is left between the rails to provide a space for the iron metal to expand and contract during the summer and winter seasons due to the change in the temperatures.
If the gap is not left in between then the rails will bend more and cause the derailing of the trains.