Italy did not "betray" Germany on WW1.
Italy had entered the Triple Alliance with Germany and Austria–Hungary in 1882, but that had always been a defensive alliance. The treaty (available here in the italian version) clearly stated that if one of the signatories was to be attacked by France (article 2) or attacked by two "big powers" (article 3) the other two signatories were obliged to help "with all available strength".
But in WW1 it was Germany that formally declared war first on Russia (on August 1, 1914) and then on France (on August 3). So the italian government claimed that the treaty did not apply.
The other answer to your question, of course, is that Germany and Italy at the start of WW2 were under very different regimes than before. The relationship between Hitler and Mussolini was very strong: even if the two were by no means always in agreement, it was very clear that they were natural allies against the democracies of France and Great Britain.
On the other hand, in 1914 this was far from the case: Italian establishment was closer to France and Great Britain (even if there had been many political and trading disagreements with France). And of course Austria-Hungary had always been the main antagonist of the Kingdom of Italy since before its creation.
That by the way meant that the Triple Alliance was more of realpolitik hack than a real alliance of like minded nations. It was really the creation of the german chancellor Bismark, and one of the instruments of his Balance of Powers policy, meant to preserve the status quo in Europe. Of course everything changed when the german foreign policy became much more aggressive under Kaiser Wilhelm II after 1890.
The way that this question has been asked makes it a bit weird to answer but I can try as it makes it seem as if the same WW1 German and the same WW1 Italian governments were leading their respective countries.
During WW1, Germany was lead by Kaiser Wilhelm the 2nd. However as we all know he was ultimately replaced by the Weimar Republic which was then replaced/taken over by the Nazi Party. In Italy, Mussolini took over the country and both countries were run by Fascist Dictatorships. These two new governments allied with each other with their own goals and objectives in the war. For all intensive purposes the two countries were using each other as means to an end and the Nazis ultimately would have attempted to conquer Italy eventually.
TL;DR: German and Italian governments shifted from Monarchies to Fascist Dictatorships which had their own plans.
The discussion has so far focused on Italy in World War I, but I can answer bit about why Germany and Italy got together during the 1930s. Many people (including me before I read up on it) assumed that since both countries were fascist and the Nazis patterned some aspects of their aesthetics on the Italian fascists that they naturally grew close. However, their initial relationship wasn't completely harmonious.
Hitler was an early and loud admirer of Mussolini, but it took a while before the admiration began to flow the other way as well. Mussolini's first visit to Germany, for instance, wasn't until 1937 when the Nazis had already been in power for four years. And there was a real fear on the Italians' part that Germany's designs on its continental neighbors would eventually extend to Italy. When the Nazis came to power, Austria existed as a buffer between Germany and Italy, but Italy was alarmed when in July 1934 the Austrian Nazis tried to take over their government. Mussolini believed that the attempted putsch had been ordered by Hitler, and he further knew that a Nazi Austria would inevitably be absorbed into the Reich, putting the Nazis on his border.
So what changed between 1934 and 1939? Italy invaded Ethiopia. In 1896, Italy had tried to conquer Ethiopia in order to expand their small colonial empire and were badly beaten by a people they considered inferior (not an uncommon attitude among the Western world towards Africans) at the Battle of Adowa. By the 1920s, Mussolini was trying to build Italy's colonial possessions up with a focus on North and Central Africa, and it just so happened that Ethiopia was between two of Italy's existing colonies in Eritrea and Italian Somalia. Mussolini saw an opportunity to avenge the memory of Adowa (thus becoming a national hero) and expanding and fortifying his empire all at once. He also thought it could be done quickly enough to bring his army back to the continent to ward off any possible attack there--which could only come from Germany. However, there was a major flaw in Mussolini's plan: Ethiopia was a member of the League of Nations, as was Italy, and the primary belief of the League of Nations was that members were not supposed to attack each other. Mussolini gambled that the other major states in the League, namely Britain and France, wouldn't really care since they themselves had huge colonies and colonial interests in Africa. Germany had withdrawn from the League in 1933 so it could pursue its rearmament program, but Hitler watched the run-up to Italy's invasion of Ethiopia and predicted the outcome rather easily: that Italy would become a pariah in Europe and internationally and would need new friends (Germany had left the League of Nations in 1933 in order to build up its military, so they didn't have to play League politics). Hitler ordered the German press to stop attacking Italy and told his ambassador to Rome that he was to tell Mussolini that Germany would be "benevolently neutral" towards Italy's attack on Ethiopia. Mussolini responded in kind by silencing Italian press attacks on Germany, ended the circulation ban on the Nazis' primary newspaper (the Voelkischer Beobachter), and stopped insulting Hitler behind closed doors (Mussolini liked to say Hitler was a sexual deviant). Interestingly, Germany also quietly sent large shipments of weapons to the Ethiopian military so as to lessen the chances of a diplomatic solution.
So Italy attacked Ethiopia in 1936 and while it was a harder fight than anticipated they eventually conquered the country. The international community heaped scorn on Italy, which culminated in Italy withdrawing from the League of Nations in 1937. Their only friend in the world was Germany. When Germany re-militarized the Rhineland in violation of the Versailles Treaty, Mussolini expressed admiration while the rest of Europe expressed grave concern. By 1938, relations were so good that Mussolini gave his blessing to the Anschluss, which created the very German/Italian border that he had feared in 1934.
So why did Germany need Italy? A major reason was the Italian navy. Germany was still building a modern surface navy (which they never truly completed) and knew it couldn't challenge Britain or France at sea. But Italy could, especially in the Mediterranean where Italy could tie down large portions of the British and French navies. Allying with Italy also removed the need to tie down German forces on a southern front, as had happened in World War I when Austria proved incapable of fighting the Italians alone (and would again happen when Italy proved incapable of fighting its own battles in World War II). It wasn't until the outbreak of the war that Italy admitted to Germany that their military preparedness was inadequate for a European conflict and had been banking on several more years of peace. Additionally, German and Italian interests didn't overlap: Germany's main focus was on central and eastern Europe, while Italy wanted southern Europe, the Balkans, Greece, and as much of the Mediterranean coast as it could get. If an alliance removed a potential enemy without having to give up one's own ambitions, why not do it? However, Germany and Italy did not formally become allies until the Pact of Steel was signed in March 1939. Germany pushed hard for it because Mussolini had intervened at the last minute the previous year to bring the European powers together for peace talks during the Sudetenland Crisis. Germany was trying to start a war and Hitler was furious when Mussolini jumped in to prolong the peace, so the Pact of Steel (which was an offensive ans well as defensive alliance) ensured that Italy would stay at Germany's side the next time Hitler wanted to start a war.
So in summary, Germany and Italy got off to a rough start, but ultimately they both became politically isolated and realized that they were ideologically compatible and working together was in their mutual interest.
Sources: A World at Arms: A Global History of World War II by Gerhard L. Weinberg and Between Two Fires: Europe's Path in the 1930s by David Clay Large.