What caused it and how do I prevent this???
Stress is the biggest killer of fish. It allows other illnesses to take hold. He was stressed and old, so he got sick. It also seems you don't know much about fish illnesses, so you probably missed the early warning signs. Fish have to be treated early if you want them to pull through.
For future fish, make sure to read up on fish illnesses before you have a problem. Keep basic fish medicines in the house.
A good forum for learning more is this one:
http://www.fishforums.net/
Well, he was 3 years old....and from what I've read, that is a feat on its own. It was his time I suppose. On to the goldfish.....
Goldfish live a long time, but they grow big and they're messy. You'll need a much bigger tank, with a full filtration system and the like. You'll need to cycle the tank, which'll take some research first. Some of the fancy types have additional health problems that you'll need to watch. Most people who keep goldfish are ill-treating them, so research... don't rely on what friends do with their fish.
Bettas are much easier to keep and much hardier.
Reading now about MelaFix...for fins regrowth....will wait to see how he behaves in a couple of days. If the meds seem to work, I'll look into the MelaFix.
Melafix is something I always keep on hand. You can use it as a mild treatment and give it preemptively. If you know you need to move a fish for example, you can give it melafix for a few days to help it fight off infection. Bettas should be given a lower dose than is recommended on the bottle. Perhaps a third to a half, depending how urgent you think it is. The same goes for other medicines that aren't betta-specific.
Let fresh water stand for 3-4 days before changing beta water & add a tiny sprinkle of salt with the 3-4 drops conditioner/qt. of water.
Aging the water by standing works if chlorine is in the water. However, some water supplies are dosed with chloramine, which doesn't disappear during aging. A chemical dechlorinator is the safest method in those areas. I couldn't find out what my local water company uses, so I don't take chances.