When you feel tired and your muscles hurt on a long ride, your fatigue is caused by running out of sugar stored in your muscles.
That's a very confident and simple explanation of what causes fatigue. The actual explanation is much more elusive:
Although it is not difficult to know when one is fatigued, it is entirely another matter to be able to identify the physiological mechanisms responsible for this condition. Despite the accumulation of a substantial literature on the topic...few principles have emerged to characterize the phenomenon of muscle fatigue. Although progress has been made in the study of muscle fatigue...we are largely unable to state with certainty why an individual becomes fatigued under various conditions.
— Enoka et al, Muscle fatigue: what, why and how it influences muscle function, 2007