Energy efficiency (EE) is critical to fully achieve the huge potentials of device-to-device (D2D) communications with limited battery capacity. In this paper, we consider the two-stage EE optimization problem, which consists of a joint spectrum and power allocation problem in the first stage, and a context-aware D2D peer selection problem in the second stage. We provide a general tractable framework for solving the combinatorial problem, which is NP-hard due to the binary and continuous optimization variables. In each stage, user equipments (UEs) from two finite and disjoint sets are matched in a two-sided stable way based on the mutual preferences. First, the preferences of UEs are defined as the maximum achievable EE. An iterative power allocation algorithm is proposed to optimize EE under a specific match, which is developed by exploiting nonlinear fractional programming and Lagrange dual decomposition. Second, we propose an iterative matching algorithm, which first produces a stabl