Same reason nations form alliances at any time - there is strength and security in numbers. The nationalist empires at the beginning of World War I allied themselves with other empires and countries that were culturally similar, or had similar goals, or if they simply shared an enemy.
Russia and France had little in common, but were both fighting Germany, for example. While Germany and Austria-Hungary shared a border, and enemies on the eastern front. Serbia and Russia share a religion - Eastern Orthodox.
In the modern day, alliances like NATO are great for smaller nations. Luxembourg, which fell in 40 minutes to Hitler's tanks during World War II, is now allied with three nuclear-armed countries, and is unlikely ever to be attacked again.