Fancy goldfish are often plagued by digestive and swim bladder health issues, which cause them to lose their ability to swim normally. Instead of maintaining a consistent orientation upright and/or a controlled depth, an affected goldfish will struggle to keep from flipping upside-down, floating up to the top of the tank where it will float for hours, even days.
Because these fish have been cultivated for centuries, bred to enhance their distended belly shapes, it goes without saying that they could be prone to problems with their internal organs. When a fish begins to exhibit this loss of equilibrium and control, it spirals around the tank upside-down floating at the top until exhaustion forces it to stop for rest. Once it is still, the goldfish will float up to the surface of the water like a tennis ball in a swimming pool, until it gets tired of floating, and tries to dive down towards the bottom once again.
There are typically two causes for this, one is bacterial, the other is purely a digestive issue. Bottom line: there's too much air inside the abdomen, and it's acting on the goldfish like a balloon. First, do a 40% water change. No matter what is happening internally to a fish, clean water makes recovery and general health much more likely. Goldfish also like a small amount of rock salt in the water. 1 tablespoon per ten gallons is fine. Do not use any other type of salt - make sure that you have true rock salt.
To distinguish what sort of internal issue you are dealing with, try this simple yet foolproof method of clearing air from the goldfish's gut, in a process of elimination of symptoms. Get a few frozen peas from the freezer, let them thaw naturally, then remove the outer peel from each pea. Break the inside bits into manageable bites for your fish and toss them into the tank. Goldfish are omnivores, and they love a good green plant-based treat. They'll gobble these up in no time.
Do this two or three times in one day, then watch your upside-down floating goldfish the next day to see if there's any change. The peas push everything through the fish's intestines, and force it all out, even the air trapped inside. If this was the problem causing your goldfish to lose control of its ballast, you will see a happy, upright and regulated fish the next day. If, however, you are greeted by the same upside-down floating goldfish, then it is likely due to a bacterial infection.
A true bacterial issue is hard to treat - the most successful method I have ever used was to feed the fish medicated food specially designed for goldfish diseases. This way the antibiotics go directly into the fish's bloodstream as they're digested with the food. Adding medicine to the water takes longer to be properly absorbed by the fish's body, and is generally less effective for swim bladder problems. If you see improvement, continue the medicated food for ten days, then slowly change back to your regular food. I strongly recommend a regular treat of the peas, it's good for the fish and they love it. Also, avoid floating pellets and granules - the goldfish swallow a large amount of air with each gulp, which is often what starts the whole problem all over again.
Keep in mind that at the end of the day, some fish are just not going to make it. It could be a genetic issue, or a birth defect, or a host of other complications that mean no matter how you treat symptoms, the goldfish will continue its upside-down floating, until eating is impossible, and the fish is slowly starving to death. If this happens, do your poor fish a favor and euthanize it humanely by placing it in a sealed plastic bag with water and air in it, then place the whole thing into the freezer. The fish falls asleep as it gets colder, and is completely unaware of what's happening.