Cloud service providers such as Microsoft and Google are beginning to power up their datacenters using multiple energy sources. To reduce cost and emission, they incorporate green energy sources into the power supply, while to improve service availability, they back up datacenters using traditional (usually brown) energy sources. However, challenge arises due to distinct characteristics of energy sources used for different goals. How to select optimal energy sources and plan their capacity for constructing datacenters to meet cost, emission and service availability requirement remains to be fully explored. This work provides a holistic solution to address this problem. We present GreenPlanning, a framework to strike a judicious balance among multiple energy sources, grid power and energy storage devices for a datacenter in terms of the above three goals. GreenPlanning investigates different features and operations of a wide spectrum of green and brown energy sources available to datacenters. The framework minimizes the lifetime total cost including both capital and operational cost for a datacenter. We conduct extensive simulations to evaluate GreenPlanning with real-life computational workload and meteorological data traces. Results demonstrate that GreenPlanning can reduce the lifetime total cost and emission by more than 50% compared to traditional configurations, while still satisfying service availability requirement.