The light propagates in a straight line but its speed depends on the medium by which it propagates. In a vacuum, it reaches its maximum speed which is c = 300,000 km / s. However, if a medium has particles, light travels more slowly. This speed reduction experienced by light is indicated by the refractive index that
it is the ratio between the speed of light in a vacuum and the speed of light in the medium in which it travels.
The higher the refractive index, the greater the speed reduction of the light that passes through that medium.
Also, when the light passes from one medium to another it changes the direction. This phenomenon is known as refraction and in order for the refractive indices of both media to occur, they must be different and the light must obliquely affect the separation surface between the media.
In the case of the pencil in the glass of water, the light is going through two media of air and water, and both media have different refractive indices, so that the light acquires different speeds and also when passing from the air to the water, it also changes direction and the effect of the split pencil is produced.