Any metal buried in soil will have a tendency to revert back to its original form, which is rust. To mitigate this natural phenomena, a proven and effective process called Cathodic protection, developed decades ago, is applied. Cathodic Protection works the same way as a car battery where the anodic side corrodes and the cathodic side is protected. The power source is provided by a rectifier. The theory is that current will leave the anodes, travel through the soil, enter the pipeline, and go back to the rectifier to complete the loop.
Corrosion only occurs at the anodes where the current leaves and enter the soil.
The pipeline, being cathodic and receiving the current, is protected. This is only a rough idea of how it works.