The short answer to your question is that it often worked. The popular conception of WW1 combat is the one you identified: soldiers climbing out of their trenches and getting mowed down immediately by machine gun fire, with the attack stagnating in no-mans land. This definitely did happen during WW1, but it also wasn't the rule.
Instead, by 1916 attacks generally managed to move past no-man's land and take the enemy first trench line. When the Germans launched their attack on Verdun in February 1916, they advanced around 2 miles in the first three days - and this was a comparatively slow advance. While the British attack at the Somme on July 1st, the first day, mostly stalled, the French also attacked and they advanced almost 2 miles in a single day. A French attack at the Somme on September 12th, 1916, took six kilometres of the German trench line (width) and in one place pushed forward 3 kilometres in a single day (depth).
Of course, on paper these numbers look small, but in order to push forward 3 kilometres in a single day, the infantry must have been able to climb out of their trenches and move past no-man's land without being mown down. The key was mostly intensive artillery bombardment before the attack. Throughout the war artillery bombardments steadily increased in strength, and no First World War fortifications, no matter how strong, could withstand the sheer firepower which was poured down by the end of 1916.
The problem mostly wasn't climbing out of the trenches without being mown down by machine gun fire, it was sustaining these attacks for a long time and it was holding on to enemy ground after it was taken. These attacks took a lot of shells and also took a lot of preparation to be successful. Once the troops took the enemy first line, there was a second line a few miles behind that. So the attackers would have to move all their guns up over shell-blasted ground, and start bombarding the second line in preparation for another assault. And as the attackers were moving up, the defenders would start reinforcing and building more trench lines further back. So the cycle repeated. Moreover, the attackers would be holding on to a blasted and destroyed first-line trench, so the defenders could counter-attack and take back the trench after the assault.
The final big problem was communication. Even if an attack was very successful, attacking troops had no reliable means of quickly relaying the news back to HQ. Radio was unreliable and in its infancy, and telephone needed wires which would get destroyed during an attack. So the best means was with runners, but by the time a runner got back to HQ and HQ began to move troops up to exploit the attack, the defenders could plug the hole and stop the breach.
This is of course simplified and there's way more to be said, but hopefully this gives you an idea that First World War combat was a bit more complicated than the attackers being mown down by machine-gun fire in no-man's land. Attackers could reliably take small pieces of ground, but couldn't exploit a successful attack and turn it into a mass breakthrough.
Books I used for this short answer:
William Philpott, Bloody Victory
Hew Strachan, The First World War
Peter Hart, The Great War
Hi, while more can always be said, this section of our FAQ may be of some interest for you.