When railway tracks are laid the engineers leave a small gap between two rails. All metals expand when heated. If two railway tracks are laid together without any gap between them they will push against each other when they expand in the day time because of the heat of the sun, and when they cool down in the night they will contract and return to their original state pulling against each other. This constant pushing and pulling against one another when they heat and expand in the day time and cool down and contract in the night will result in the weakening of the joints between the two tracks and after a few days the two tracks may also break free from one another. Such a situation will result in the derailment of the trains causing major accidents and loss of lives.
So, the railway engineers always leave a small gap between two rails to compensate for the expansion of the rails during the hot day time and contraction during cold nights.
The gaps you are referring to the small gaps that are left between section of the rail that form each side of the parallel railways for over which railway engines, coaches, trains and trams run.
These gaps which are of the order of a few millimeters, are provided to allow room for the rails to expand the rise in temperature due to the atmospheric temperature as well as the friction caused by running of train. All materials expand when heated and contract when cooled. the extent of expansion and contraction is quite low to be noticed by people or make much difference in most situations. However, since each section of the rail is very long, the expansion due to heating is significant, and unless space is provided for expansion of rails, the rails will bend to accommodate the increased length. This will make the railway unfit for running of trains.