Why do railways have a gap?

contrary to what many people suggest, the gaps are not there for the purpose of allowing expansion and contraction to occur. They exist, because segments of rail have traditionally been required to be removed and replaced easily and so had to be made a manageable length. The other reason gaps exist in the rail it to allow for expansion and contraction caused by the movements of trains over them. They have fishplates located between the joins to force the rails back into lateral alignment and thus prevent derailments.
In modern continuously welded rail these gaps are often cut on a diagonal to prevent this misalignment. The thermal expansion and contraction of rail track is minimal and the forces push to the outside rather than along the rail