Think of frequency as a counting rate, it tells you how many complete wave cycles are generated by the source every second. This rate is decided entirely by whatever is generating the wave, such as a vibrating tuning fork for sound or an oscillating charge for light, and has nothing to do with the medium the wave later travels through. So as a wave crosses from air into water, or from one optical medium into another, the number of cycles produced per second cannot suddenly change, because the source on the near side is unaffected by what happens further along the wave's path. What does change is how fast each of those cycles can travel through the new medium, since speed is controlled by the medium's properties like density and elasticity for sound, or refractive index for light. Because speed changes while frequency is locked by the source, and the two are tied together through $v = f\lambda$, the wavelength has to adjust to keep the equation balanced. This is why frequency is the one property, out of speed, wavelength and frequency, that never changes when a wave changes medium.