Cloud radio access networks (CRANs) make it possible to reduce power consumption in future 5G networks by decoupling baseband units (BBUs) from cell sites and centralizing the baseband processing from remote radio-heads (RRHs) in BBU pools in a cloud. Although this centralization can enable power savings, it imposes much higher traffic on the optical transport network used to connect RRHs to the BBU pool, i.e., the fronthaul. In this paper, we propose a hybrid cloud-fog RAN (CF-RAN) architecture that resorts to fog computing and to network function virtualization to replicate the processing capacity of a CRAN in local fog nodes closer to the RRHs that can be activated on demand to process surplus fronthaul/cloud traffic. We devise an integer linear programming (ILP) formulation and graph-based heuristics to decide when to activate fog nodes and how to dimension wavelengths on a time-and-wavelength division multiplexing passive optical network to support the fronthaul. Our results show that our architecture can consume up to 96% less energy than a traditional distributed RAN, providing a maximum transmission latency of about 20 μs between RRHs and BBUs even in large traffic scenarios. Moreover, we demonstrate that our graph-based heuristics can achieve the same optimal solutions of the ILP formulation but with a reduction of 99.86% in the execution time.