While every organism doesn't necessarily have to serve an obvious beneficial purpose to an ecosystem, coral is a vital part of the reef ecosystem. Coral literally creates the reef. Many corals grow by depositing calcium they take out of the water into their skeleton. This skeleton remains even if the organism dies, so over long periods of time, layers of dead coral skeletons provide the structure that makes up the reef. Live corals grow off of dead skeletons, fish hide in the cracks, algae grows on it, which is eaten by fish, snails, hermit crabs, and many others. The large surface area allows bacteria to grow which filter the water. In many cases the coral reefs protect nearby shorelines from the worst of the waves, prevent erosion. As mentioned, many organisms also directly consume corals. Some live within the corals as well, for example some crabs have symbiotic relationships with corals. The crabs eat the mucus produced by the coral (doesn't harm the coral), and the crab protects the coral from predators like some starfish that eat and kill corals. I could go on and on.