Wastewater treatment plants (WWTPs) offer opportunities to optimize resource utilization and enhance energy efficiency. This study provides a comprehensive analysis of using the polygeneration approach in WWTPs to reduce grid energy dependence, optimize energy distribution, and utilize surplus energy for hydrogen (H2) and ammonia (NH3) production. Several models were employed, including photovoltaic (PV) cells, parabolic trough collectors (PTCs), steam methane reforming, and polymer electrolyte membranes, to assess the feasibility of this approach. Three scenarios were evaluated and compared: Scenario 1 (Baseline) represents the current situation, Scenario 2 maximizes the Net Present Value (NPV), and Scenario 3 minimizes NH3 production costs. Real data from As-Samra WWTP in Jordan was used to accurately assess the feasibility of each scenario. The results show that Scenario 2 offers the highest profitability and efficiency, with a NPV of 87.48 million USD and an annual NH3 production of 15,417 tons, reducing both grid dependency and biogas fuel consumption. Both Scenarios 2 and 3 demonstrate the ability to meet thermal demands efficiently while generating significant revenue from NH3 production. Scenario 3, in particular, achieves competitive H2 and NH3 production costs. Environmentally, Scenario 2 significantly reduces annual greenhouse gas emissions by 12.66 kilotons of CO2eq, with near-zero carbon intensity for thermal energy due to solar reliance. In conclusion, the polygeneration approach offers a promising pathway for WWTPs to achieve greater sustainability, economic gains, and reduced environmental impact, providing valuable insights for decision-makers.