Short disclamer at the beginning: This question cannot receive an absolute answer, because the answer is as heterogeneous as coastal areas are worldwide. To answer concretely, you need to focus on a concrete example. But here is a very simplified solution:
First of all, the moderate climates that exist near the coast are determined by the heat capacity of the water. Simplified, this means that water absorbs a lot of heat in the warm periods and thus cools the air temperature and releases this stored heat again in the cold months!
How great the influence is depends on many factors. For example, the topography. For example, if we have a mountain range near the sea, this moderate weather is hardly noticeable in the Innlad. But also winds etc. determine how much this heat absorption and heat emission of the water influences the ambient air.
An impressive area where a very dry area borders the ocean and thus disproves your gerneralised statement is the Namib Desert.