When were trenches first used and what qualities enabled trenches to be used in WWI? Also why only 20 years later, in WWII, did they almost cease to exist? I mean did they ever really cease to exist? Because I know that the Iraq-Iran war was fought in trenches and had qualities, such as chemical weapons, that were much similar to WWI. Why were trenches still used (rarely) in some wars much later than 1918, but not used only 20 years later in WWII?
In the First World War, the first trenches appeared in 1914. WW1 wasn't the first war to utilise tench works though, they appeared drying the US Civil War as well as the Russo-Japanese War some ten years before WW1. The Russo-Japanese War actually have a pretty good indication for what the world could expect from future wars, entrenched positions and heavy artillery.
In the First World War, trenches were the logical counter to the use of machine guns and artillery, when the air is filled with exploding shrapnel shells and lead bullets, the safest place to be was below ground level. The first trenches no doubt were created by linking together personal foxholes. I'm on mobile so I can't easily link it but there is a picture of French soldiers occupying a trench that barely fits a single person standing abreast of it. Trenches such as these were clearly dug hurriedly.
If you search, there are numerous answers explaining why the line of trenches stretched from the English Channel to the Swiss border so I won't repeat them but I will say that trenches were no absent from the Second World War. Just about every prepared defensive position from that war belonging to all sides would have featured some variety of earthwork. The German defences at Normandy, the US positions in the Ardennes, the Russian defences on the Eastern Front etc. the main difference between these trenches and those that the Allied and German armies occupied twenty years earlier was that for the most part, they weren't occupied for prolonged lengths of time and this comes down to the shift in the balance of weaponry. During the first years of the First World War, weapons technology strongly favoured the defence and it wasn't until the last year and a half that Allies really possessed the means to overcome German defences. By the Second World War, weapons technology had sufficiently advanced to overcome most defences, armoured vehicles were highly mobile and could be used to quickly exploit any breakthrough and aircraft had advanced sufficiently to play a critical role in ground attack. What this meant was that static defences such as trenches were no longer suitable for preventing enemy assaults as they could be quickly outflanked and overrun by a coordinated attack.
Perhaps someone else will elaborate on this, but trenches were used heavily on the Eastern Front in WWII. I've read somewhere that Soviets' trenches, if put together in one straight line, would extend as far as from Moscow to Spain.
Generally speaking, trench warfare was something that just happened. With increasing firepower and industrialization, most powers thought that the war will be short. The idea was to "get there firstest with the mostest". But because of the arms race, almost every major European power possessed the same technology. After everyone realized that the war will be bloody, require men power, and everybody had the same weapons, the logical thing was to trench in, so the WWI became a war of attrition.
Speaking of the lack of trench warfare after the WWII. It is mostly because the 'total war' was proven to be costly. Nobody wanted to repeat the same thing over and over again, WWI and WWII was enough. The guerrilla warfare has prevented the 'conventional' warfare as well and it's efficacy.
I am rusty on the warfare history but that's the general idea of it. I can recommend "Utility of Force" by R. Smith, which is a good book that touches on warfare from Napoleonic wars all the way to counter-insurgency. And that's his main argument. Conventional and total war is costly, guerrilla warfare has been successful for some countries who weren't able to withstand conventional war (Vietnam, Afghanistan), to combat this we have counter-insurgency tactics and the focus on smaller units.