In a nutshell, the European powers were allied into two powerful and opposing blocs by military and political treaties. Germany, the Austro Hungarian Empire and the Ottoman (Turkish) Empire on one side (with other allies), and France, Britain, Russia and Italy (and others) on the other. The Ottoman Empire - Turkey, the Balkans and North Africa was starting to creak and break up. All of the other nations named above, and others, were trying to annex bits of the Ottoman Empire to add to their Empires. They all had greedy Imperial ambitions. Prussia, and then Germany as it became had defeated France in a war in 1871 and was growing very powerful. Britain, France, and Russia were all concerned about German expansion. Austria Hungary and Germany were worried about Russia.
So the network of political and military treaties was intended as a deterrent for all the countries - attack us and you are attacking my allies too.
But as Turkey became weaker and weaker, and lost conflicts in the Balkans in the early 20th century, Austria Hungary saw a golden opportunity to snatch bits for its own empire. So it annexed Bosnia Herzegovina, and Bulgaria, and was looking at others too. Serbia was grabbing bits too though, and Austria Hungary began to view them as a threat after Serbia annexed some Adriatic ports. But none of the other powers were prepared to go to war over Serbia, so nothing happened - although things were tense - until Archduke Ferdinand of Austria was assassinated in Bosnia by a Serb. Then Austria Hungary issued an ultimatum (ludicrously harsh) to Serbia. The Serbs couldn't, or wouldn't agree, so Austria Hungary attacked.
Then Russia vowed to defend the Serbs, and declared war on Austria Hungary
Germany - defending its ally, declared war on Russia
France, supporting its ally Russia started mobilising troops
Germany could see itself being outflanked, so declared war on France, and occupied Belgium to knock France out of the war quickly
Britain declared war on Germany, to support France and meet its obligation to maintain Belgian independence
And then everyone was at war.