There are three separate periods of time where Wormtail would've had different reasons to stay:
- Between Wormtail's faked death and Sirius' escape from Azkaban
- Between Sirius' escape from Azkaban and Sirius' break-in to Hogwarts
- Between Sirius' break-in to Hogwarts and Pettigrew's reveal in the Shrieking Shack
1. Between Wormtail's faked death and Sirius' escape from Azkaban
The reason that Wormtail didn't leave during this period is clear. As @alexwlchan stated, Wormtail's reason for finding a Wizarding family is given by Sirius:
Why else did you find a wizard family to take you in? Keeping an ear
out for news, weren’t you, Peter? Just in case your old protector
regained strength, and it was safe to rejoin him...
As Sirius explains, Wormtail went to stay with the Weasleys because he wanted to stay close to a source of information. Maybe his plan was initially to stay at the Burrow and hear news from Arthur and Molly, but if one of the Weasley children decided to take him to Hogwarts, he would be able to do nothing about it. Even if his initial plan was to stay at the Burrow, Hogwarts was still a great option, since it would not only be a great source of information but would also provide protection, in case anyone did come after him. During this time, he had no reason to leave Hogwarts, as he had exactly what he wanted: information and protection (not to mention food and shelter).
2. Between Sirius' escape from Azkaban and Sirius' break-in to Hogwarts
During this period, there was no reason for Wormtail to leave Hogwarts. He knew that Sirius had broken out of prison, but not that Sirius knew where he was. Wormtail still thought he was well-hidden, so there was no reason to leave the castle to go find some other hiding place (which would most likely be less protected than Hogwarts anyway).
3. Between Sirius' break-in to Hogwarts and Pettigrew's reveal in the Shrieking Shack
This seems to be the time that would make the most sense for Wormtail to have run away. However, it's important to note that Wormtail didn't know how Sirius had managed to find him. The one day in the past twelve years that Sirius had gotten the newspaper happened to be the one day that it pictured the Weasleys on the cover and Wormtail happened to be pictured in it with a clear view of the paw with a missing finger. It's an extremely low probability situation that Wormtail would never have even considered. From Wormtail's perspective, Sirius, who was out of Wormtail's league in terms of intelligence and magical ability, had pulled off something really impressive by managing to find him. So he probably thought that if Sirius was able to find him at Hogwarts, he could find him wherever else he decided to hide. At least at Hogwarts he would be protected by the castle's physical defenses, the heightened security (including dementors), and the professors.