The best way to avoid it is to stay outside of its sensor cone. Clear datum immediately after revealing your location or being fired on (which implies that they know your location).
If it does lock onto you...
In the stock game, placing a decoy between you and the torp will do the trick just about every time, since the torp explodes when it hits the decoy. To improve your chances of getting the torp to lock up the decoy, turn across the LOS between the torp and the decoy; this will force the torp sensor to sweep over the decoy when it turns to lead your new course.
With LW/Ami, decoys only attract torps, they don't destroy them, so you must either drop decoys and scoot far enough away that you are out of range when the torp reaches the decoy, or manuever rapidly enough that when the torp passes the decoy you are outside its sensor cone.
As for knowing where they are, you know the same way you know where everything else is--TMA! As a shortcut, you can use UUVs to triangulate the location, either in pairs or with ownship sonar data. If you know the location of the shooter its easy to enter an initial TMA solution which will suffice for a range estimate over the long term, while using sensor data to provide bearing. In intial TMA solution is often easy even when you don't know the location is was fired from, since there's a very good chance you can mark it with two sonar arrays and get a halfway decent triangulation.
__________________
|