Tunnel warfare
Pre-Gunpowder: 

Antiquity: 

Ancient Greece: The Greek historian Polybius, in his Histories, gives a graphic account of mining and counter mining at the Roman siege of Ambracia:

The Aetolians ... offered a gallant resistance to the assault of the siege artillery and [the Romans], therefore, in despair had recourse to mines and tunnels.  Having safely secured the central one of their three works, and carefully concealed the shaft with wattle screens, they erected in front of it a covered walk or stoa about two hundred feet long, parallel with the wall; and beginning digging from that, they carried it on unceasingly day and night, working in relays. For a considerable number of days the besieged did not discover them carrying the earth away through the shaft; but when the heap of earth thus brought out became too high to be concealed from those inside the city, the commanders of the besieged garrison set to work vigorously digging a trench inside, parallel to the wall and to the stoa which faced the towers. When the trench was made to the required depth, they next placed in a row along the side of the trench nearest the wall a number of brazen vessels made very thin; and, as they walked along the bottom of the trench past these, they listened for the noise of the digging outside. Having marked the spot indicated by any of these brazen vessels, which were extraordinarily sensitive and vibrated to the sound outside, they began digging from within, at right angles to the trench, another tunnel leading under the wall, so calculated as to exactly hit the enemy's tunnel. This was soon accomplished, for the Romans had not only brought their mine up to the wall, but had under-pinned a considerable length of it on either side of their mine; and thus the two parties found themselves face to face.
The Aetolians then countered the Roman mine with smoke from burning feathers with charcoal. - In essence an early form of chemical warfare.
Another extraordinary use of siege-mining in ancient Greece was during Philip V of Macedon's siege of the little town of Prinassos, according to Polybius, "the ground around the town were extremely rocky and hard, making any siege-mining virtually impossible. However, Philip ordered his soldiers during the cover of night collect earth from elsewhere and throw it all down at the fake tunnel's entrance, making it look like the Macedonians were almost finished completing the tunnels. Eventually, when Philip V announced that large parts of the town-walls were undermined, the citizens surrendered without delay."
Polybius also describes the Seleucids and Parthians employing tunnels and counter-tunnels during the siege of Sirynx.

Roman: The oldest known sources about employing tunnels and trenches for guerrilla-like warfare are Roman. After the Revolt of the Batavi, the insurgent tribes soon started to change defensive practices, from only local strongholds to using the advantage of wider terrain. Hidden trenches to assemble for surprise attacks were dug, connected via tunnels for secure fallback. In action, often barriers were used to prevent the enemy from pursuing.
Roman legions entering the country soon learned to fear this warfare, as the ambushing of marching columns caused high casualties. Therefore, they approached possibly fortified areas very carefully, giving time to evaluate, assemble troops and organize them. When the Romans were themselves on the defensive the large underground aqueduct system was used in the defense of Rome, as well as to evacuate fleeing leaders.
The use of tunnels as a means of guerrilla-like warfare against the Roman Empire was also a common practice of the Jewish rebels in Judea during the Bar Kokhba revolt (132–136 AD). With time the Romans understood that efforts should be made to expose these tunnels. Once an entrance was discovered fire was lit, either smoking out the rebels or suffocating them to death.
Well-preserved evidence of mining and counter-mining operations has been unearthed at the fortress of Dura-Europos, which fell to the Sassanians in 256/7 AD during Roman–Persian wars.

China: Mining was a siege method used in ancient China from at least the Warring States (481–221 BC) period forward. When enemies attempted to dig tunnels under walls for mining or entry into the city, the defenders used large bellows to pump smoke into the tunnels in order to suffocate the intruders.

Post-classical: In warfare during the Middle Ages, a "mine" was a tunnel dug to bring down castles and other fortifications. Attackers used this technique when the fortification was not built on solid rock, developing it as a response to stone-built castles that could not be burned like earlier-style wooden forts. A tunnel would be excavated under the outer defenses either to provide access into the fortification or to collapse the walls. These tunnels would normally be supported by temporary wooden props as the digging progressed. Once the excavation was complete, the attackers would collapse the wall or tower being undermined by filling the excavation with combustible material that, when lit, would burn away the props leaving the structure above unsupported and thus liable to collapse.
A tactic related to mining is sapping the wall, where engineers would dig at the base of a wall with crowbars and picks. Peter of les Vaux-de-Cernay recounts how at the battle of Carcassonne, during the Albigensian Crusade, "after the top of the wall had been somewhat weakened by bombardment from petraries, our engineers succeeded with great difficulty in bringing a four-wheeled wagon, covered in oxhides, close to the wall, from which they set to work to sap the wall".
As in the siege of Carcassonne, defenders worked to prevent sapping by dumping anything they had down on attackers who tried to dig under the wall. Successful sapping usually ended the battle, since the defenders would no longer be able to defend their position and would surrender, or the attackers could enter the fortification and engage the defenders in close combat.
Several methods resisted or countered undermining. Often the siting of a castle could make mining difficult. The walls of a castle could be constructed either on solid rock or on sandy or water-logged land, making it difficult to dig mines. A very deep ditch or moat could be constructed in front of the walls, as was done at Pembroke Castle, or even artificial lakes, as was done at Kenilworth Castle. This makes it more difficult to dig a mine, and even if a breach is made, the ditch or moat makes exploiting the breach difficult.
Defenders could also dig counter mines. From these they could then dig into the attackers' tunnels and sortie into them to either kill the miners or to set fire to the pit-props to collapse the attackers' tunnel. Alternatively they could under-mine the attackers' tunnels and create a camouflet to collapse the attackers' tunnels. Finally if the walls were breached, they could either place obstacles in the breach, for example a cheval de frise to hinder a forlorn hope, or construct a coupure. The great concentric ringed fortresses, like Beaumaris Castle on Anglesey, were designed so that the inner walls were ready-built coupures: if an attacker succeeded in breaching the outer walls, he would enter a killing field between the lower outer walls and the higher inner walls.

Coming of gunpowder: A major change took place in the art of tunnel warfare in the 15th century in Italy with the development of gunpowder, since its use reduced the effort required to undermine a wall while also increasing lethality.
Ivan the Terrible took Kazan with the use of gunpowder explosions to undermine its walls.
Many fortresses built counter mine galleries, "hearing tunnels" which were used to listen for enemy mines being built. At a distance of about fifty yards they could be used to detect tunneling. The Kremlin had such tunnels.
Since the 16th century during assault on enemy positions saps, began to be used.
The Austrian general of Italian origin Raimondo Montecuccoli (1609–1680) in his classic work on military affairs described methods of destruction and countering of enemy saps. In his paper on "the assaulting of fortresses" Vauban (1633–1707) the creator of the French School of Fortification gave a theory of mine attack and how to calculate various saps and the amount of gunpowder needed for explosions.

19th century: 

Crimean War: As early as 1840 Eduard Totleben and Schilder-Schuldner had been engaged on questions of organisation and conduct of underground attacks. They began to use electric current to disrupt charges. Special boring instruments of complex design were developed.
In the Siege of Sevastopol (1854–1855) underground fighting became immense. At first the allies began digging saps without any precautions. After a series of explosions caused by counter mine action the allies increased the depth of the tunnels but began to meet rocky ground and the underground war had to return to higher levels. During the siege Russian sappers dug 6.8 kilometres (4.2 mi) of saps and counter mines. During the same period the allies dug 1.3 kilometres (0.81 mi). The Russians expended 12 tons of gunpowder in the underground war while the allies used 64 tons. These figures show that the Russians tried to create a more extensive network of tunnels and carried out better targeted attacks with only minimal use of gunpowder. The allies used outdated fuses so that many charges failed to go off.