Distributed energy systems can produce low-cost utilities for large energy networks, but can also be a resource for the electric grid by their ability to ramp production or to store thermal energy by responding to real-time market signals. In this work, dynamic optimization exploits the flexibility of thermal energy storage by determining optimal times to store and extract excess energy. This concept is applied to a polygeneration distributed energy system with combined heat and power, district heating, district cooling, and chilled water thermal energy storage. The system is a university campus responsible for meeting the energy needs of tens of thousands of people. The objective for the dynamic optimization problem is to minimize cost over a 24-hour period while meeting multiple loads in real time. The paper presents a novel algorithm to solve this dynamic optimization problem with energy storage by decomposing the problem into multiple static mixed-integer nonlinear programming (MINLP) problems. Another innovative feature of this work is the study of a large, complex energy network which includes the interrelations of a wide variety of energy technologies. Results indicate that a cost savings of 16.5% is realized when the system can participate in the wholesale electricity market. HIGHLIGHTS A district energy system with central cooling, heating, and electricity generation is studied The system is optimized over 24 hours using thermal energy storage to shift loads A novel static/dynamic decomposition is used to solve the dynamic optimization problem Scenarios with buying and selling electrical power in a real-time market are considered Overall, a savings of 16.5% is achieved for a one-year period