The immediate cause of the war was the assassination of the Austrian Archduke Franz Ferdinand in 1914. Franz Ferdinand was the heir to the Habsburg throne, a respected Austrian politician who also happened to be friends with the German Kaiser. Ironically, he was also the politician in the Austrian state with the most direct control of the Austrian military, and he tended to favor peaceful diplomacy over the potential outbreak of war – it is possible that he would have been a prominent voice for peace if he had survived. Instead, he was assassinated not by Austria's rivals Russia or France, but by a young Serbian nationalist.
Serbia was a new nation. It had fought its way to independence from the Ottoman Empire in 1878, and its political leaders envisioned a role for Serbia like that Piedmont had played in Italy: one small kingdom that came to conquer and unite a nation. In this case, the Serbs hoped to conquer and unite the Balkans in one Serbian-dominated country. Austria, however, stood in the path of Serbian ambition since Austria controlled neighboring Bosnia (in which many Serbs lived as a significant minority of the population). Thus, the last thing Austrian politicians wanted was an anti-Austrian movement launched by the ambitious Serbs.
In 1903, a military coup in Serbia killed the king and installed a fiercely nationalistic leadership. Serbian nationalists were proud of their Slavic heritage, and Russia became a powerful ally in large part because of the Slavic connection between Russians and Serbs (i.e. they spoke related languages and the Russian and Serbian Orthodox churches were part of the same branch of Christianity). Russia also supported Serbia because of Russian rivalry with Austria. Serbian nationalists believed that, with Russian support, it would be possible to create an international crisis in Austrian-controlled Bosnia and ultimately seize Bosnia itself. The Serbs did not believe that Austria would risk a full-scale war with Russia in order to hold on to Bosnia.
Among the organizers of the coup that had murdered the king and queen were a group of Serbian officers who created a terrorist group, The Black Hand. In 1914, The Black Hand trained a group of (ethnically Serbian) college students in Bosnia to assassinate an Austrian politician when the opportunity presented itself. That happened in June of 1914, when Franz Ferdinand and his wife came to visit the Bosnian capital of Sarajevo. In a fantastically bungled assassination, Franz Ferdinand survived a series of attacks, with some of his would-be killers getting cold feet and running off, others injuring bystanders but missing the Archduke, and others losing track of where the Archduke's motorcade was. Finally, quite by accident, the Archduke's driver became lost and stuck in traffic outside of a cafe in which one of the assassins was eating a sandwich. The assassin, Gavrilo Princip, seized the opportunity to stride outside and shoot the Archduke and his wife to death.
Serbia's assumption that Austria would not risk war proved to be completely wrong. The Austrian government demanded that Serbia allow Austrian agents to carry out a full-scale investigation of the assassination; Serbian honor would never allow such a thing. Austrian troops started massing near the Serbian border, and the great powers of Europe started calling up their troops. Germany, believing that its own military and industrial resources were such that it would be the victor in a war against France and Russia, promised to stand by Austria regardless of what happened. Russia warned that Austrian intervention in Serbia would cause war. France assured Russia of its loyalty. Only Britain was as-yet unaccounted for.
No one was completely certain that a war would actually happen (the German Kaiser, Wilhelm II, left for his summer vacation as planned right in the middle of the crisis, believing no war would occur), but if it did, each of the great powers was confident that they would be victorious in the end. A desperate diplomatic scramble ensued as diplomats, parliaments, and heads of state tried at the last minute to preserve the peace, but in the end it was too late: on July 28, Austria declared war on Serbia, activating the pre-existing system of alliances, and by August 4 all of the great powers were involved.
Thanks to the fact that Germany invaded through Belgium, Great Britain declared war on Germany and its allies. In addition to Germany and the Austrian Empire, the Ottoman Empire soon joined their alliance, known as the Central Powers. Opposing them was the Triple Entente of Great Britain, France, and Russia. Smaller states like Italy and Portugal later joined the Triple Entente, as did, eventually, the United States.