The electric power system is undergoing dramatic transformations due to the emergence of renewable resources. However, integrating these resources into the electric grid has proven to be difficult for two main reasons: these resources are uncertain and distributed. This thesis discusses how uncertainty and distributedness of the new resources are manifested as challenges in time and spatial scales, and how we can optimally control the power grid to overcome these challenges. The theme of this thesis is that in order to control the new resources, we need a better understanding of the physical power flow in the system. To illustrate the spatial-scale challenge, we consider the voltage regulation problem for distribution networks. With a deep penetration of distributed energy resources the voltage magnitudes in a distribution system can fluctuate significantly. To control these resources such that voltage profiles remain flat, we need to coordinate the tens of thousands of households in the distribution network. By studying the geometry of power flows in the network, we show that even though the voltage regulation problem is non-convex, it can be convexified exactly through a semidefinite relaxation . Based on this insight, we an optimal and decentralized algorithm for this problem. Only communication between electrical neighbors are required in the algorithm, thus allowing it to scale to problem of large sizes.