Hydration and Electrolyte Balance for Endurance Athletes

I’m a pharmacist and an avid hiker, so I’m curious about how marathoners manage hydration and electrolytes without overdoing it. What’s the best way to stay balanced during those long events?