The condition your fish has is called Swim Bladder Disorder (sbd) which is obviously a disorder that affects a fish's swim bladder. The swim bladder is responsible for a fish being able to swim upright and maintain its buoyancy. When it becomes blocked, infected or too full of air it ceases to function properly as you are witnessing with your fish.
Many types of goldfish are highly susceptible to this disorder mainly because of the odd shapes of some species, genetics and poor breeding practices.
There really does not exist a fool-proof way to prevent sbd but by maintaining excellent water conditions and soaking flake food prior to feeding can lower the prevalence of the disorder. The reason for soaking flake food, prior to feeding, in tank water set aside, is that the flakes will absorb water thus lessening the amount of air the fish intakes when gulping the food
The traditional way to treat this is by isolating the affected fish in a quarantine tank, if possible, using water from the main tank so as to not change the fish's environment too much.
Once seperated begin to feed the fish canned peas without the husk. Feed 2-4 peas twice a day making sure to clean out any uneaten portions. Peas are well known to clear out a fish's swim bladder.It's best to feed the pea diet exclusively. If the pea diet does not work then try the pea diet with intermitent days of fasting.
In addition to the pea diet you can try treating the fish with a medicine called Maracyn-Two which is sometimes helpful in curing sbd as some cases are caused by bacteria. It can easily be purchased at most pet stores where fish are sold. Follow the directions on the product label for dosage amounts. For a view of Maracyn-Two, click on the 1st link below. Note: if you are located outside of the USA please let me know and I will recommend a medicine more eaily found.
It would also be helpful to not fill the quarantine tank up to the top with water leaving about 2-3 inches from the top free as this will lessen the amopunt of water pressure on the fish.
Please keep in mind that sbd can sometimes be very difficult to cure and is one of the
most common problems goldfish keepers face.
Listed below are 2 additional links which will further elaborate on sbd.
Best wishes and please let me know if you have any questions.