Why did European nations form alliances?
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.
There were a couple main reasons for this in WWI. First, there was ethnicity. Second, there was strategic positioning. Both of these were important.
As an example of ethnicity, the Serbs were allied with the Russians. This was really mostly because both were Slavs. The same reason led Germany and Austria-Hungary to be allies.
As for strategy, the main example of this was the alliance between France and Russia. They were allies because they were both afraid of Germany. So they got together in hopes that their alliance would intimidate Germany.
European nations formed alliances for three reasons: economic gains, political and security stability, and social cohesiveness. Due to their locations in Europe, some countries without access to any ports or bodies of water, looked to tradeto bring into the country what its citizens needed and desired. Also because of the many countries located within a small geographic area, political and security played an important role in seeing many countries, monarchies and single family rule, maintain and survive. Lastly since many peoples had similarly ethnicity, it was in their interest to form alliances to make sure they had every possible advantage.