This paper develops an algorithmic framework for real-time optimization of distribution-level distributed energy resources (DERs). The proposed framework optimizes the operation of both DERs that are individually controllable and groups of DERs (i.e., aggregations) that are jointly controlled at an electrical point of connection. From an electrical standpoint, wye and delta single-and multi-phase connections are accounted for. The algorithm enables (groups of) DERs to pursue given performance objectives, while adjusting their (aggregate) powers to respond to services requested by grid operators and to maintain electrical quantities within engineering limits. The design of the algorithm leverages a time-varying bi-level problem formulation capturing various performance objectives and engineering constraints, and an online implementation of primal-dual projected-gradient methods. The gradient steps are suitably modified to accommodate appropriate measurements from the distribution network and the DERs. By virtue of this approach, the resultant algorithm can cope with inaccuracies in the distribution-system modeling, it avoids pervasive metering to gather the state of non-controllable resources, and it naturally lends itself to a distributed implementation. Analytical stability and convergence claims are established in terms of tracking of the solution of the formulated time-varying optimization problem. The proposed method is tested in a realistic distribution system with real data.