The creation of these alliances was a complicated process, but it revolved largely around German actions.
Germany wanted its "place in the sun." For that reason, it did things like increasing the size of its fleet and pushing for more of a role in colonial affairs in incidents like the Moroccan crises of 1905 and 1911. When Germany did these sorts of things, it scared the British and the French. As these countries became more worried about Germany, they developed a closer and closer alliance with one another. This alliance came to include Russia.
Once this alliance was complete, Germany was forced into a tighter alliance with Austria-Hungary. The two empires first allied themselves largely for ethnic reasons as both were ruled by Germans. They were also both worried about Russia and so had made a pact in 1879 promising to support one another in the event of a Russian attack on either. Once France, Britain and Russia came into a tighter alliance, Germany was left with no real ally other than Austria-Hungary. For that reason, the two empires had to become closer because they had no other options. This closeness was reinforced in episodes like the Bosnian Crisis of 1908.
So, the system of alliances was created largely by two factors. These were British, French, and Russian fear of Germany and (after those countries allied) the fear that Germany and Austria-Hungary had that they would become isolated and be at the mercy of the Triple Entente.
one reason was that a few countries (germany predominantly) were jealous of the British and French empires. This produced tension and rivalry between these countries. Britain and france felt they needed strength so became allies with each other and russia.