Whenever you see some trait in an animal, you need to think about why that trait would help the animal to survive or why it would help the animal to be successful in reproducing. (Unless it is a domesticated animal where people have selected for traits that they want). This is because natural selection only selects traits that do those things.
With most animals, the reason to be colorful is to attract a mate. Being colorful can help the animal to be seen by potential mates. In addition, the colors can serve as something of a sign of health. If an animal of the opposite sex sees a colorful animal, it knows that animal is healthy and would likely be a good mate. (Good meaning that it would have good genes to pass along).