I am aware that soldiers spent significant amount of time pinned in trenches by enemy while simultaneously pinning that enemy in their own trenches with no man's land between them.
But how did those trenches got made? I would understand that the first army on the battlefield had opportunity to dug them up in relative peace but the opposing army had to dug them up while under fire, no?
Well, you technically asked two separate questions, so I'll answer them separately.
First: How did those trenches get made?
Simple. While trench warfare became infamous after WW1, military field works (such as trenches) have existed since the advent of large-scale war. Since ancient times, armies have dug trenches, established pickets, and erected barricades whenever they were encamped. At the start of WW1, soldier simply did what they've always done. They advanced towards the enemy until they were close enough to observe the enemy but not close enough to engage in combat, and then encamped in preparation for the battle the next day.
Prior to WW1, an army would then leave camp (usually in the morning), array themselves opposite of the enemy forces, and then they would advance towards each other, inevitably clash, and a victor would emerge.
But WW1 was different. This is because the technological advances in sheer firepower severely outpaced technological advances in combat mobility. Specifically, the improved rate of fire of WW1-era weapons like the Vickers machine gun and fast-firing heavy artillery like the French 75 compared to previous eras changed everything. Just imagine fighting against machine guns and artillery, but without modern armor like tanks or air support.
The thing is, many of the armies participating in WW1 were very poorly prepared for the advent of machine guns and fast-firing heavy artillery entering the theater of war en masse. The military doctrine of the nations that fought in WW1 had not caught up to advances in military technology. This is particularly evident when you observe that many WW1-era armies still fielded large numbers of cavalry, which were hitherto considered an integral part of warfare alongside infantry and artillery.
When the battle started, like it always did, the opposing armies soon found themselves suffering because the fundamental equation of war they relied upon had changed drastically. The most important discovery was that the cavalry had lost their traditional role, and could no longer compete against infantry armed with machine guns and fast-firing heavy artillery that could rain hell upon their positions. Artillery could even "outrun" cavalry from great distances away due to the advent of radio communications.
As a result, they retreated to their encampments, where they probably then realized being entrenched reduced the number of casualties. So they started moving the trenches as close as they possibly could, probably in tandem. Eventually, the trenches got so close that once they established the closest possible distance and could go no further, the zone between the two trenches became the no man's land. Once they advanced until they could go no further, they begun started to attack from the 'side' of the enemy trenches, in outflanking maneuvers. This series of outflanking maneuvers, especially between Germany and France, was called "the race to the sea". Eventually, the outflanking stopped because the trenches ran out of gaps between them to actually outflank. This was when the war of attrition that WW1 is infamous for began in earnest.
So you could make the assumption that trench warfare started because cavalry had failed in its role as a mobile shock force, and the lack of mobility on all sides resulted in trench warfare. It was only until tanks were invented to replace cavalry, and aircraft in the military became more prevalent, that trench warfare stopped being the norm. On an unrelated interesting note, this is why many tank/armored unis are referred to as cavalry units - because that was their intended replacement role.
Second: The hypothesis that the first army on the battlefield had the opportunity to dig trenches in relative peace, while opposing forces arriving later needed to dig trenches under fire.
This is true, to some extent. However, you assume all the battlefields were the same, when they were not. Trench-building was most dominant on the Western Front. Your hypothesis has one army arriving way ahead of the other. However, at least for France vs Germany, what really happened was that the opposing armies were mirroring each other from the beginning of the conflict. The battle was not decided from the onset, but the opposing Franco-German forces were already marshaled against each other. Yet, they realized frontal assaults were mostly futile. So, trench-style outflanking started, per the so-called "race to the sea".
But your hypothesis is certainly correct in some instances, for if the first army to arrive had the opportunity to fortify in relative peace, the opposing army would in fact have to dig trenches under withering fire. This occurred during the attack on the Ottoman Empire (the Gallipoli Campaign) which saw Commonwealth troops deployed that had to travel by sea before they landed on Turkey. In this case, the Ottoman defender was already entrenched, while the attacker certainly was not. The resulting amphibian landings proved disastrous, and the Gallipolli Campaign ultimately failed after 8 months of fierce-fought conflict.
Sources:
Ellis, John (1977), Eye-Deep in Hell – Life in the Trenches 1914–1918, Fontana <--- (I really recommend this book btw)
Griffith, Paddy (2004), Fortifications of the Western Front 1914–18, Oxford: Osprey
Keegan, John (1999), The First World War, New York: Alfred A. Knopf
Broadbent, Harvey (2005). Gallipoli: The Fatal Shore. Camberwell, VIC: Viking/Penguin
Hope this helped!
Simple answer:
First part of the war was still a mobile war. The Germans tried to advance to Paris but got stopped at the battle of the Marne. In order to rescue their armies they had to withdraw and regroup. There wasn't a plan B so the German soldiers started to dig in on top of hills, behind rivers, in forest, .. (places that were easy to defend) The opposing side quickly started doing the same after several bloody attacks on the enemy lines.
One part of the front, though was still heavily contested and that was the Northern part of France and the West of Belgium.
The Germans tried to force a breach and conquer the harbors in Northern France to make coordination, supply etc between the allied forces more difficult. They launched an offensive at the last remnants of the Belgian army who with French and English support (and flooding the Yser valley) held back the german army.
From the Belgian coast (especially after the flooding) till the swiss frontier the front was in a deadlock, neither side had any experience with a modern trench warfare and winter was coming.
Both sides had to lots to learn, unfortunatly every lesson during a war is paid in blood.