The 24-year-old Welsh athlete, who was third in the standings at the start of the event, was penalised for not placing her wetsuit in the box during the transition between the swimming and bike legs.
But Stanford, who won the Under-23 world title last year, recovered superbly in the running leg to win in a time of two hours one minute and 32 seconds, which took her to the top of the standings with 4,220 points.
"Being the world champion, it's crazy, it hasn't really sunk in yet," Stanford, who became the first woman to add the world championship title in the year directly after winning an Under-23 crown, said.
"I'm just trying to take it my stride, but when I get five minutes to reflect on it I will probably get quite emotional.
"I just can't believe it to be honest, I can't quite put it into words. All the hard work over the last year, I've just managed to hold it together and it's paid off."
Stanford, who recovered from a broken arm she suffered during a fall in the sixth leg in Stockholm, built an early lead in the run and took her penalty just before the final lap.
Her fellow Briton Jodie Stimpson finished second overall after coming fourth in London's Hyde Park and Germany's Anne Haug took third place.
Just 13 points separated the top three ahead of Saturday's race with Gwen Jorgensen of the United States leading Haug followed by Stanford.
Pre-race favourite Jorgensen made a good start in the swimming but crashed out during the bike leg in slippery conditions which eventually forced her to retire.
Haug lost two minutes to the leaders during the swimming and failed to make up the lost time.
Ireland's Aileen Reid finished second in the London race with Australia's Emma Moffatt third.