Substances contract or expand when the ambient temperature changes. The change is determined by the substance's thermal coefficient of expansion or contraction. Most of the time, this expansion or contraction is too small to be observed by the naked eye. In the case of railways, we leave small gaps (on the order of few millimeters) so as to accommodate the expansion and contraction of the rails. During the day time, due to higher ambient temperature, the rails expand; while at night, due to lower ambient temperature, the rails contract. If there was no spacing, the expansion of rails during warm periods might cause them to push against each other and destabilize the joints, thereby putting the trains at risk of derailment (and subsequent loss of property and life).
Hope this helps.