It’s supposedly based on the track of Roman chariots. But it’s kind of indirect and convoluted, so Snopes rates this as a mixture. Roman chariots were standardized and designed to accommodate two horses, side by side, pulling the chariot. Those chariots created rut in roads. Later wagons roughly matched that track so their axles wouldn’t break. Early railroad equipment was built by wagon builders who were accustomed to the gauge.
Other gauges were common in the early stages of railroad development. A wider gauge would probably have been more efficient for steam locomotive boilers because the boilers could have been larger around, yet closer to the ground for a better center of gravity. But the majority of railroads in England were built to 4’ 8” gauge and Parliament dictated that future lines be built (in England, but not Ireland [5’ 3”/1600mm] and did not mandate a gauge for the various British colonies) to that gauge for more efficient interchange between rail lines.
When Northern US railroads were being built, they inaccurately assumed that many of the locomotives would be imported from England. Much of the US South built to a 5’ gauge, but there were relatively few interchanges making an incomplete overall system. After the Union won the war and returned the South into the Union, the Southern railroads were rebuilt to the standard gauge. Legislation approving a ‘transcontinental’ railroad from the US Midwest to the Pacific Coast mandated the standard gauge be used.
Throughout much of the world, similar factors influenced adoption.
In much of sub-Saharan Africa, theoretically cheaper narrow gauge (3’ 6”/1067mm) was adopted, but is not well suited to hauling the raw minerals that are the major cargo. Brazil uses a Meter gauge (1000mm/3’ 3⅜”) and Australia has a mixture of gauges in different states (including Irish gauge, standard gauge, and narrow gauge).
BART runs on a wide gauge (5’ 6”/1676mm) in part to provide ‘first class seating,’ something that is very noticeable when you transfer to either SF Muni Metro or Caltrain (which are both standard gauge). BART pays a heavy price because almost all of its equipment has to be custom built or modified to function (including maintenance equipment). But the Toronto Transit Commission uses 4’ 10⅞”/1495mm gauge on its streetcar and subway lines.