The oil in your car is pumped through the engine when it's running, protecting it from mechanical wear. Oil is sold in a variety of "weights," usually expressed as xWy. The difference between 10W30 and 10W40 oil isn't too big, but to understand it, you'll have to understand a few basic things about fluid viscosity and the behaviour of oils under different temperatures.
The numbers in a name like 10W30 refer to an oil's "weight." This is a measurement of the oil's viscosity -- how thick it is, and how quickly it moves. Lower weights mean a thinner oil; higher weights mean a thicker one. In a car's engine, a thinner oil is more useful at first, when the car's just started and the oil must flow quickly through the engine. But as the engine heats up, a too-thin oil can be a problem.
The viscosity of a liquid like motor oil varies with temperature. As the oil heats up, it becomes less viscous and flows more easily, thanks to the increased motion of the molecules making it up. Since oil runs through engines and therefore heats up all the time, it will constantly change its viscosity, and this needs to be taken into account when buying or categorising oil.
The change in viscosity with temperature presents a problem: since you want a thin oil when the engine is cold, but not a too-thin oil when it's hot, how do you find an oil that won't thin out too much as the car runs? Multi-weight oils, like 10W30 and 10W40, contain long-chain polymers that expand and contract with temperature changes, altering the way the oil behaves. In this way, an oil can be thin to begin with, but not as thin at higher temperatures than its original viscosity would suggest.
In the weight measurement of a multi-weight oil, the number before the W expresses the oil's weight in cold conditions, and the second number its weight at temperatures of over 37.8 degrees C Centigrade. Multi-weight oils are still thinner when hot than they are when cold, but a 10W30 oil, for example, will only be as thin when hot as a 30-weight oil, whereas it will be as thin when cold as a thinner 10-weight oil.
10W30 vs. 10W40
Both these oils have the same weight when cold, and it's a good weight, sufficiently low to get the oil moving through the engine quickly. A weight of 30 when hot is very common, and ideal for many engines -- but if you're having problems with engine wear or leaks, the 10W40 oil will provide more protection for a running engine, and escape through leaks more slowly.