it has a swimbladder problem.
there are 3 main causes of swimbladder problems in goldfish
1) physical injury; think how hard it is for us to breath with cracked or broken ribs,
the same goes for a fish, if it has injured its' body in such a way that the swimbladder can not inflate/deflate properly
it looses its balance and often turns upsidedown.
only time will heal this problem, there is nothing you can do.
2) intestinal blockage; again if the fish is constipated then the build up of waste is affecting its abiltiy to regulate its swimbladder.
feeding it a cooked, deshelled, garden pea will help as this acts as a laxative and flushes the system out as will bloodworm.
to prevent this kind of problem in the first place, only feed your goldfish sinking foods. often when feeding from the surface they
gulp in air and that can become lodged in their digestive tract.
3) internal bacterial infection; you can get an antibacterial medication that may or may not help.
if you are in the UK Interpet do a med just for this, but IMHO, their internal bacterial med is far better
HTH