Italy was an interesting faction in the first World War. It was the original member of the triple alliance 1882 made up of Austria-Hungary, Germany and, of course, Italy itself. Despite a pact between the three of them, that they will protect each other’s country with supporting military might, they had plenty of internal conflicts. Austria-Hungary was in dispute with Italy over territorial right - an awkward relationship really. The pact even agreed that Italy would not join the fray if any of them was facing the Great Britain. Then in 1902, Italy secured a secret diplomacy deal with France, which ensured Italy would be a peace pact between the two of them as well.
Fast forward to the beginning of World War One in 1914, Germany was going to war against the Russia and France, while Austria-Hungary was fighting against Serbia. Due to the partnership putting Italy in a strange position, with all the diplomatic pacts it was in, Italy decided to stay neutral. Its justification for it was that the triple alliance was meant to be a defensive pact, and Italy was not obliged in assisting its allies in their own war since they were the ones who took the offensive.
Some speculated that Italy was just being a bystander, waiting for an opportunity to gain from the potential victors. On April 26, 1915, the Italians once again in secrecy signed the Treaty of London, this agreement would have Italy leaving the triple alliance for a large section new territory, an offer to good to pass up. On May 3rd, Italy backed out of the triple alliance, and on the 23rd of the same month, it declared war on their historical enemy and past ally of the Austria-Hungary Empire.
So a simple answer to why did Italy switch sides in WW1, Italy was just seizing up the perfect time to take over the reign of their main opponent, probably seeing a better future with its new allies.