Equestria is an artificial Garden/Gaia world, and is encased in a mini Dyson Sphere. The Sun, Moon and stars are lights on the ‘ceiling’ which is too far up for even Pegasi to truly notice. Celestia and Luna are the physical avatars of the AIs that control these lights.
Most life on Equestria was genetically engineered by some advanced civilisation that harvests Harmony as an energy source, which is produced by cooperation between disparate peoples and species. The Tree of Harmony collects and stores this energy, most of which is periodically harvested covertly, but some is left behind to power the planet’s systems.
The Elements of Harmony are a failsafe that can be used to ‘correct’ aberrant life and rogue systems that threaten Equestria by concentrating Harmony energy as a weapon. All the magic in the series is the result of nanomachines, or Equestrians co-opting planetary systems to their benefit, such as Pegasi manipulating the planets inclement weather systems.