A flange is only flat while the pipe behind it holds the shape it was welded in. When that pipe is cut, extended or re-routed, the load it puts on the flange changes, and a face that mated cleanly before now sits at an angle. The gasket cannot take up that angle, so the joint leaks.
The leak is usually blamed on the gasket. Replacing it fixes the symptom for a while, then the new gasket takes the same set as the old one and the leak returns, which is the point at which the flange itself has to be looked at.
With the joint apart and the gasket out, lay a straight edge across the flange face and look for light underneath. Check across one diameter, then across the other. A face that is out shows a gap on one side and contact on the opposite side, and the pattern tells you which way the pipe is pulling.
Check the mating flange the same way. It is common for both faces to be slightly out in different directions, which is why the joint was only just sealing before the pipe behind it was disturbed.
Where the flange is sound and the pipe has moved, the repair is at the weld, not at the face. Cut the weld back carefully, free the flange, and bring the faces together with the system hanging in its mounts so the flange finds its own position against its mate.
That is easier said than done with a thin disc in a tight space, and it is why many of these jobs end with the flange removed entirely. If the weld is cut, the flange must be cleaned of the old seam before it is re-welded, or the new joint sits on contaminated metal.
A flange with slight distortion, no deep pitting and enough thickness can be re-faced. The work is done on the face itself, taking material off evenly until the straight edge sits flat in both directions, then checking the thickness so the flange still has something to clamp against.
This is a workshop job rather than a driveway job. Doing it by hand with a file is possible on a thick flange carrying a small error, but the result is only as flat as the patience behind it, and a face that is flat one way and not the other will leak again.
A flange that is cracked, badly pitted, or has been re-faced before is normally replaced. The new one is aligned on the pipe with the joint bolted up to its mate, tacked in that position, and only then welded out, so the face ends up where the car wants it rather than where the bench put it.
Bolt the joint together before welding rather than after. It holds the face square to its mate while the heat goes in, and any movement caused by the weld is resisted by the joint rather than absorbed by the gasket afterwards.
The last step is to confirm the joint closes without force. If the faces need drawing together with the bolts, the flange is still out and the gasket is carrying a load it cannot survive. A joint that closes by hand and then takes a final nip is the only one worth leaving.
Where the section behind the flange has been replaced, matching the wall helps more than anything else. The exhaust pipe sizes offered with a given system are made to sit with the flange that belongs to it, and a repair on a stainless steel exhaust system should use material from the same family rather than a scrap of something else.