Trench warfare dates back hundreds of years. It was an effective method of attacking a fortress. During the early age of artillery, the attacking forces tried to move their artillery close to a fort in order to breach the walls. The trenches were just a way to get closer without exposing the cannoneers to fire from the walls of the fort.
The earliest trenches were built in the zig-zag pattern in order to make it harder to fire into the trenches from the side. This was seen in the World War I trenches.
Trench warfare became a frequent military tactic for defending territory between the US Civil War (1861-1865) and the first World War (1914).