The integration of renewable energy and flexible energy sources in buildings brings numerous benefits. However, the integration of new technologies has increased the complexity and despite the progress of optimization algorithms and technologies, new research challenges emerge. With the increasing availability of data and advanced modeling tools, stakeholders in the building sector are actively seeking a more comprehensive understanding of the implementation and potential benefits of energy optimization and an extensive up-to-date survey of optimization in the context of buildings and communities is missing in the literature. This study comprehensively reviews over 180 relevant publications on the management and optimization of energy flexibility resources in buildings. The primary objective was to examine and analyze prior research, with emphasis on the used methods, objectives, and scope. The method of content analysis was used to ensure a thorough examination of the existing literature on the subject. It was concluded that multi-objective optimization is crucial to enhance the utilization of flexible resources within individual buildings and communities. Moreover, the study successfully pinpointed key challenges and opportunities for future research, such as the need for accurate data, the complexity of the optimization process, and the potential trade-offs between different objectives.