There are three types of coral reef: fringing reefs grow close to the shore in shallow water; barrier reefs grow parallel to the shore but are separated from it by a deep channel which may be several miles wide; and atolls which are circular islands of coral enclosing a lagoon, often hundreds of miles from any other land.
There are numerous theories to explain the formation of these types of reefs. The most popular is that one type developed from another in a gradual transition so that all three are different stages of the same process. If a landmass with a fringing reef begins to sink into the sea, as long as the coral can grow at an equivalent rate to the subsidence, the reef will grow further and further from the coast. Eventually a barrier reef is formed some way from the shore. If the process continues and the landmass disappears altogether, a ring of coral will be left forming an atoll. This is the simplest explanation for the formation of reefs; the actual processes involved are more complex.