P-3C orion of Pakistan navy.
The long boom at the back is a magnetic anomaly detector.
The aircraft already has stored data about Earth's magnetic field. The boom compares the earth's magnetism with stored data while flying over the sea. Any large metallic object will disrupt the magnetic field.The boom at the back detects the change caused in earth's magnetic field , pinpointing a submarine.
That large metallic object can be deep in the sea such as a submarine.
However it may be noted that such mode of submarine detection is very resource intensive and can take too long as the aircraft has to fly in a grid pattern over large oceanic areas.
For that reason this aircraft and it's magnetic system of detection is only used when the submarine has gone quiet or is too silent to be detected by Sonars, or is used in conjunction with Sonars.
Once sonars detect the submarine and an approximate location is established then these aircrafts are sent to pinpoint the submarine and finish the job.