You can think of an electromagnetic wave as the ticks of a clock. The clock ticks at a constant rate, and the wave peaks travel away from the clock, each tick corresponding to a wave peak.
If you could stand next to the train of waves as it goes by, and count how many pass you per nanosecond, it must necessarily be equal to the number of ticks per second emitted by the clock -- otherwise ticks would be getting lost (or gained).
Of course, this depends on your own clock running at the same rate as the clock at the source of the waves. If your own clock is running slower, so your measured nanoseconds are longer, then you will count a larger number of wave peaks per nanosecond than would be counted using the clock at the wave source.
As you probably have read, time runs slower the deeper you go into a gravitational well (e.g., toward a star). In that case, an electromagnetic wave falling toward the star will gain frequency as it falls. To an observer deep in the well, the number of wave peaks passing per nanosecond will be increased compared to the number of ticks per nanosecond measured at the "clock" (the source of the waves). Conversely, if the source is deep in the well and you are far away, the waves will pass you at a rate that is reduced compared to the clock rate you would measure at the source. This difference in the rate of time is a major component of the time/space distortion we call gravity.
During refraction light moves slower, but the number of ticks/waves per nanosecond can't change (if gravitational time dilation doesn't occur). The only way that light can be slower but carry the same number of wave peaks per nanosecond is for the peaks to be closer together: that is, for the wavelength to be shorter, in the refractive medium.