A critical aspect of keeping goldfish is maintaining water quality. Unbelievably important; I would postulate that 99 out of 100 goldfish deaths are directly the result of water quality problems. A fish may die of Ich or fin rot, but the root cause of these diseases in the first place is often the stress caused by poor water quality.
The general approaches to maintaining water quality and what to measure are covered elsewhere on this and other web pages. For this section, we will concentrate on nitrate, which is one of the main waste products that accumulates in a properly running aquarium or pond.
The figure above is a rough diagram of what increases the level of nitrates in you tank water. Bacteria breakdown proteins into ammonia and goldfish directly excrete ammonia from their gills (about 70 to 80% of their total ammonia output) and in their liquid vent excretions (the remainder). More bacteria oxidize ammonia into nitrite and then nitrite into nitrates.
In the figure, the red arrows indicate things that contribute to the nitrate level and the green arrows are things that lower the nitrates. The "stars" are things that deserve particular attention, since we have some control over these. Here are some more sources of information on these: