A sonar is basically a device that emits a sound wave and measures the time it takes to return to the boat after covering a certain distance.
In a boat, the sonar can be used to measure the distance of an object in the sea or the depth of the ocean. The sonar emits the sound wave, and this wave travels until it reaches the object (or the bottom of the ocean), then it travels back to the boat.
The speed of sound v in the water is known, and so by measuring the time t the wave takes to return to the original point, it is possible to infer the distance of the object (or the bottom of the ocean) from the boat:

where S is instead twice the distance:

, because the wave covered two times the distance between the boat and the object (back and forth).