Why were trenches used in World War 1?

Trenches were used in world war1 for shelter and to outflank your opponents.

Trenches were formed on the eve of the invention of the machine gun. Mainly to shelter from a hail of bullets which can literally cut a man in two.they can also offer so much protection that they can prevent people from dieing to a degree