A sand bar is formed when there is a bay. Where there is no river flowing into the sea, sand can be deposited faster than it is removed.
Longshore drift moves sand across the bay. Shallow water allows deposited sand to build up.
The deposited sand eventually joins up with the other side of the bay and blocks off the water. This feature is called a sand bar and the area behind is known as a lagoon.