A 10W40 oil is a multigrade oil that has two viscosity or flow grades. Prior to the development of multigrade oils, drivers had to use a thicker oil in summer and a thinner oil in winter.
Multigrade oils contain a polymer which slows the rate of thinning as oil temperature rises. The polymer also slows the rate of thickening as oil temperature falls.
A thinner oil will circulate faster through the engine on cold start. A thicker oil will protect better at high temperatures.
W stands for winter. The oil must have a specified maximum viscosity at low temperature. The lower the W number, the thinner the oil. A 10W oil is thicker than a 5W oil in the winter.
The number 40 means that at 100 degrees Centigrade or 212 degrees Fahrenheit, the viscosity falls within given limits. At that temperature, a 40 weight oil remains thicker than a 30 oil.
Engine makers specify oils according to considerations like engine design and temperature conditions. Engine modifications may require a change in viscosity grade.