Counter battery fire
Background: Indirect fire was introduced so that artillery could fire from behind cover to reduce its exposure to enemy artillery by making itself more difficult to find. While armies were doing this, little thought was given to the need for counter-counter measures. Perhaps the only means of finding concealed guns was observation from kites or balloons. However, effective counter-battery fire needs far more than a single method of observation. Counter-battery (CB) fire emerged and developed extremely quickly during World War I. Since that war, CB has continued to evolve, mainly due to improvements in technology.
The targets of CB fire are usually the enemy's guns, launchers and mortars, both the materiel and the men serving them. The formal NATO definition of the term counter-battery is "fire delivered for the purpose of destroying or neutralising the enemy's fire support system", with the note that it may be proactive or reactive. This may be achieved by attacks on any part of the field artillery system. In some armies at some periods CB has been called counter-bombardment, and occasionally counter-mortar has been handled separately.

Functions: There are four functions in the system for CB fire:

Target acquisition
CB intelligence
CB fire control
CB fire units

Target acquisition: Target acquisition is the source of information for CB intelligence. It may produce accurate locations for enemy fire units or merely inputs to a more complex process for locating and assessing hostile artillery. At the end of World War I, the following were recognised as the principal sources of artillery intelligence, this seems to be in descending order of usefulness:

Aeroplanes (i.e., visual observation)
Aeroplane photography
Survey sections (i.e., flash spotting)
Sound ranging sections
Balloon observation
Ground observers (artillery and intelligence posts of other arms)
Liaison officers (artillery at infantry brigade HQs, these obtained reports of enemy artillery activity)
Officers' patrols
Secret agents and epatries
Captured documents and prisoners' statements
Listening sets (i.e., monitoring enemy communications)
Intercepted wireless (by wireless compass stations)
Apart from balloons and officers' patrols, these sources continued to play their part in World War II, and their technology improved, although flash spotting became less useful as ranges increased and flashless (or low flash) propellants became widespread. A successor to officers' patrols had an isolated emergence in Italy when Canadian artillery observers were put ashore behind German lines and established themselves to observe gun positions.
Sound ranging and flash spotting both required enemy guns to fire. Furthermore, other methods such as radio direction finding and information from prisoners are insufficiently precise to "fix" a target for artillery attack. Information from others may not be received quickly and hence be out of date, the hostile battery having moved.
These methods were joined by radar in World War II; while this could detect a shell in flight the gun that fired it could not usually be seen and the shell's elliptical trajectory made it impossible to extrapolate backwards with the technology of that time. However, mortar bombs have a parabolic trajectory (as do guns firing at high angles) defined by a simple mathematical equation with two points on the parabolic curve. It was therefore possible to deduce a mortar's position by tracking its bomb and recording two points on its trajectory. Another method that emerged was crater examination, this could reveal the azimuth back to the hostile gun or mortar and study of fragments could reveal its type. However, while it was a useful source of information it was not sufficiently accurate to give a location for the firer.
Most armies abandoned flash spotting in the 1950s. However, several new target acquisition technologies emerged. These included:

UAVs, about 1960 an Unmanned Air Vehicle, the SD-1, entered artillery service. This early UAV used wet film photography by day or night, had short range and short endurance. However, being under artillery control they were responsive to CB needs, which was just as well because other forms of air reconnaissance were becoming less available and were not notably timely. Other UAVs, including drones (flying a programmed course) duly emerged, including the ability to transmit imagery in real-time.
Next, in the 1970s Hughes Aircraft developed the US Firefinder RADAR system and created the algorithms that could extrapolate a gun's position from a segment of an elliptic trajectory. It's likely the Soviet Union created similar algorithms.
Non-communications ELINT, which can detect and locate radars, including those used by artillery is an often overlooked source.
A few armies established artillery observation patrol units to operate in likely artillery deployment areas behind the enemy's forward units.
On the modern battlefield various radars are able to detect vehicles or stationary guns on the ground, although this is far from a perfect information source. Look-down radar from high altitude aircraft are able to detect vehicles over a very wide range, but are unable to determine what type of vehicles they are and are susceptible to radar reflectors and similar countermeasures. The information is useful but requires further sources of information to accurately determine which contacts are the target. Millimeter wave radar (such as the AH-64 Apache's Longbow Radar) are able to very accurately detect the types of vehicles observed but are much shorter ranged.
The arrival of highly networked combat systems allows for data from multiple sources to be cross referenced very quickly. As a result, modern counter battery fire is generally as a result of a wide array of different possible information sources working together to provide targets in close to real time.
Sound ranging systems have also evolved with newer technology, such as Hostile Artillery Locating (HALO) and similar systems developed in other countries.

CB intelligence: CB intelligence applies the intelligence cycle and principles to CB. It uses information about hostile artillery from all sources to maintain detailed records and apply specialist techniques that exploit the nature of artillery fire to produce:

Intelligence about hostile artillery positions
The enemy artillery order of battle
Intelligence about hostile artillery activity and deployment and assessments of its wider implications
CB intelligence is usually combined with CB fire control (see below), although intelligence purists recognise this is not good practice and the two were separate in the British forces in France in World War I. In both World Wars CB intelligence and CB control were found to be most effective when they were at corps level. However, the final year of World War 2 showed that the counter mortar battle was really one for brigade level. Since that war CB has tended to move to lower levels and in some armies has grown into a wider deep supporting fire organisation.

CB fire control: It does not always make tactical sense to attack hostile batteries the moment they are located. This is magnified by the challenges of targeting hostile batteries. There are many factors, and their significance depends on the circumstances. The first issue for targeting is that it is difficult to knock out a battery, although smart munitions against SP guns may change this.  As the quoted definition states, "destroy" is one possibility; another is "neutralization": to render the battery temporarily ineffective or unusable, including by suppressing it or forcing it to move. However, "suppression" only lasts while CB fire is falling, and if a hostile battery moves then it has to be found again. Sometimes it is best just to record the location of the hostile battery and leave it for later. 
An additional issue for the use of counter battery fire is the finite artillery resources available for use in a given situation.

CB fire units: The final aspect of the CB equation is having available CB fire units and appropriate munitions. Typically these are general supporting fire units, but direct supporting fire units are also used if they are available and not fully occupied by their primary role. With conventional HE shells it may require the concentrated fire of 5–10 batteries to deal effectively with one hostile battery. Hence a value of multiple rocket launchers is their ability to deliver a heavy and concentrated attack from relatively few launchers.

Counter-measures: Counter-measures to CB fire have emerged throughout history. These include:

Digging in. In World War I, even heavy artillery was dug-in with several feet of overhead protection. Even today North Korean artillery is widely thought to be resistant to CB fire because of its deeply entrenched positions. More generally precision munitions have decreased the value of digging.
Encasing guns in armour. Fully armoured self-propelled guns were introduced to provide protection against conventional HE (High Explosive) fire.
Shoot-and-scoot tactics, in which a self-propelled artillery vehicle or towed gun fires a single round or salvo and immediately begins moving. Shoot-and-scoot tactics were first used in World War II by Soviet Katyusha rocket launchers.
Spreading out. Increasing the dispersion of guns in a position has been aided by computers for technical fire control. Introduction of guns with self-survey and orientation has led to the concept of gun maneuver areas where the troops, platoons or sections of a battery keep moving around, although it is questionable how sustainable this is.
Concealment. While firing guns cannot escape sound-ranging and radar detection, concealment and deception can reduce the likelihood of discovery from other methods.
Counter-battery fire, being prepared to reply to enemy counter-battery fire with counter-battery fire of your own.
Human shields. The practice of embedding artillery assets within a civilian population to discourage enemy counter-battery fire, based on the assumption that a counter-battery strike would damage and destroy civilian infrastructures as well as killing innocent non-combatants. The legal protections afforded to non-combatants mean such tactics would generally constitute a war crime.
Of course there are many potential target nodes in the field artillery system, including those dedicated to finding hostile artillery.