The bottleneck in state-of-the-art thermoelectric power generation and cooling is the low performance of thermoelectric materials. The main difficulty is to obtain a large thermoelectric power factor as the Seebeck coefficient and the electrical conductivity cannot be increased independently.Here, relating the thermoelastic properties of the electron gas that performs the thermoelectric energy conversion, to its transport properties, we show that the power factor can diverge in the vicinity of the metal-to-superconductor phase transition in two-dimensional systems. We provide experimental evidence of the rapid increase of the Seebeck coefficient without decreasing the electrical conductivity in a 100-nm Ba(Fe1−xCox)2As2 thin film with high structural quality, as the sample temperature approaches the critical temperature, resulting in a power factor enhancement of approximately 300. This level of performance cannot be achieved in a system with low structural quality as shown experimentally with our sample degraded by ion bombardment as defects preclude the strong enhancement of the Seebeck coefficient near the phase transition. We also theoretically discuss the thermoelectric conversion efficiency for a wide-range of model systems, and show that driving the electronic system to the vicinity of a phase transition may be an innovative path towards performance increase at the possible cost of a narrow temperature range of use of such materials.