Surely each would be able to fire at each other while the trenches were being dug, particularly if one side had dug in first. How did this happen?
On the Western Front, parts of the trench systems were designed to maintain close contact with enemy trench systems or defensive positions, and were by necessity built close to enemy forward positions to fulfil this operational tactic. The trenches of opposing forces being very close together was not actually very common, but forward positions and listening posts were. These types of positions were not meant to be occupied consistently, as a reserve or support trench would be. Forward outposts in no-mans-land were commonly unsupported by communication trenches, these were often fortified or 'splinter proof' camouflaged shell-holes, which were relatively easy to construct in the field.
In general, German positions were sited in good tactical positions, as being in enemy territory they could withdraw to suitable areas. Allied forces were often tasked with holding ground gained at all costs however, including holding inferior ground, such as trenches near enemy lines. These were sometimes constructed by joining together shell hole emplacements under fire, and then communication trenches dug out to the new line. Early on the French developed a system known as 'Roulement', which rolled units through the forward trench systems to rear lines and then billets out of harms way. This was soon adopted by the British and the Germans. Trench systems, while considered static, were always under development and being updated and expanded, and held by new personnel.
'Saps' were trenches dug towards enemy positions into unoccupied territory to listen and observe for enemy activity. No-mans-land was quite heavily patrolled in certain areas, mostly at night, searching for enemy forward positions, listening posts and infiltrators. Trenches were constantly degrading due to weather and combat, with some systems having changed hands numerous times, and bearing the features of British, French and German designs. No-mans-land was also strangely featureless and confusing, with few landmarks and heavy use of camouflage. From ground level it was hard to believe that thousands of men were nearby, or that an enemy trench or position was just meters away. It was not uncommon for even experiences soldiers to become lost and wander into enemy lines.
As the war progressed trench warfare developed and changed, in particular on the Western Front. Trench lines were parts of large defensive systems, with rows of supporting trenches, communication trenches and forward positions. The complexity of this system cannot be over stated, with the length of the western front estimated at around 700km. If you estimate that most of this system was in some areas three trenches deep that makes, over 2000km of trenches not including communication and other trenches, for just one side of the conflict.
Trenches were also positioned in active and quiet areas, depending on the zone of activity and time of year. Quite areas would tolerate dangerously nearby positions more-so than in more disputed areas. All sides however suffered continuous casualties throughout the war regardless of the positioning of trenches, ferocity of the fighting or training of the soldiers.
In early 1917 the Germans withdrew from their forward trench positions to take up a defensive network of pillboxes, fortified shell holes and support trenches well behind their former positions. This became known as the Hindenberg line (Siegfriedstellung). The network of strongpoints no longer resembled a traditional trench system and offences into this system had to take over these positions, which were not suitable for large scale troop deployments for future operations. The static trench to trench combat had developed into what was called 'open warfare', which differed still from maneuver warfare, but took the shape of offensive against a series of strongpoints rather than directly against enemy trenches.
British tactics would not allow 'retirement' from positions gained, even when these positions were not advantageous, leading to positions dangerously close to the enemy. Not all trenches systems were in highly contested areas.
SS143: Instructions for the Training of Platoons for Offensive Action, 1917
Notes for Infantry Officers on Trench Warfare. March 1916.
Trench Warfare: The Live And Let Live system by Tony Ashworth
The Trenches. Fighting on the Western Front in World War 1 by Dorothy and Thomas Hoobler.
Distance between trenches depended a lot on the terrain and how much either side was willing to fight for a position. The whole point of a trench in the first place is to keep the occupants safe from fire, so as long as those in the trenches could continue to (relatively) safely dig forwards, they could push the trench up as much as they wanted. Generally digging forward towards enemy trenches would involve zig-zagging digs so as to prevent enemy positions from having the potential for a clear line of fire down the length of the entire trenchwork. Where terrain comes in is where you start getting points where another entrenched position overlooks the advancing one, allowing the overlooking position to fire down into the trench itself. At that point, it really ends up depending a lot on how much everyone involved wants to fight over that specific position. Worst case, well-placed trenches with good fields of fire and overlooking positions could create a situation where any opposing trenchworks within reasonable firing range of guns and light artillery/mortars is going to be completely untenable. In better situations, attackers can get very close, although you start to run the risk of bombs, grenades, and even rocks being thrown at extremely close ranges as well as the possibility of leaving your own trench network open for easy assault.
You also have to consider morale, as that tended to be a massive part of how fighting in trenches would actually end up. Gallipoli had a few interesting extremes. The ANZAC forces, for example, suffered from continuous casualties and barely tenable positions for much of the campaign because the Ottomans had positions overlooking many areas. The French forces in the campaign, on the other hand, lucked out in that their sector consisted of both a position overlooked by enemy positions as well as one that did the same to the Ottomans. After the fight bogged down with no end in sight, the French and Ottomans in the trenches ultimately came to a de facto truce over those areas, as any attempt to force the issue was going to result in huge numbers of casualties for everyone involved for negligible gain.