A novel methodology is proposed for the synthesis of polygeneration systems in tertiary sector buildings with detailed thermal integration. The methodology involves a systematic approach that combines Pinch Analysis, mathematical programming, and the definition of a superstructure with thermal flexibility whereby mass flows can exchange heat in various temperature intervals. With the detailed characterization of the thermal energy flows associated with the thermal energy technologies and services to be supplied to the building, the optimization procedure provides a more realistic system configuration, ensures that thermodynamic principles are satisfied, and allows for synergies and potential benefits to emerge. The methodology is first introduced through a simple example of a gas engine-based energy system, highlighting the necessity of a detailed characterization of the hot and cold flows regarding their quantity and quality levels. Then, the approach is applied to the case study of a Brazilian university hospital that requires electricity, steam, hot water, and chilled water. The optimization is formulated as a multi-period mixed integer linear programming model that minimizes the total annual cost of installing and operating the system using local-based data. The results show the technical and economic interest of deploying cogeneration gas engines to cover electricity and thermal energy services. Besides, a strong synergy is observed between the cogeneration gas engine and the single-effect absorption chiller. Thus, it is demonstrated how a preliminary analysis of thermal integration opportunities must be an integral part of the optimal synthesis of energy supply systems.