Even though the Sun is much farther away from the Earth than the Moon, it exerts a stronger gravitational pull because it is so huge. However, the sun exerts a smaller tidal influence on Earth than the moon.
The reason for this is that there is very little difference in the sun's gravitational pull on the surface of the Earth compared to the center of the Earth. This is because the Earth's diameter is such a tiny fraction of the distance between the Sun and the Earth.
Earth is much closer to the Moon, and there is a significant difference in the Moon's gravitational pull on different parts of the Earth.
This is what leads to tides. The Moon pulls the water (which is on the near-side surface) more than it pulls the Earth itself, which leads to a high tide. On the far-side surface the Moon's pull is less than at the center, so the Moon basically pulls the Earth away from the water forming another high tide.