Does it seem sick?
Sometimes goldfish gulp air, which can get into their digestive tracts and throw off balance. You can see air pockets even in their poop if this happens
Sometimes they actually swim vertically when they scavenge the bottom of the tank, but go back to normal afterwards. My goldfish used to keep it up for a good 20 minutes while sucking rocks then act like normal again after
But if your fish looks fat/bloated, and is swimming strangely, the swim bladder may be affected
Swim bladder stabilizes the fish when it swims, and if something is causing infection, it may lean to one side, fall over, or even float upside down