Here is my simple way of thinking about the relationship between smaller apertures and deeper depth of field. It's all about angles.
First, let's clear up one myth that might get in the way. People sometimes think that each part of their image goes through its own part of the lens. In the illustration below, they imagine that the eyes goes through the upper part of the lens and the mouth goes through the lower part of the lens. This is wrong.
For all practical purposes, every part of the image goes through every part of the lens. This is why small scratches and nicks on your glass don't show up as spots on your picture. As seen in this illustration, the eyes go through the top and bottom of the lens (and all points in between) and so does the mouth. The lens then focuses all those streams of light back to their respective places in the picture.
If we move the sensor or lens out of focus, the streams of light coming from the eye in the picture don't get focused back to a single spot. Instead of the "eye" appearing as a single spot on the sensor, it is smeared across a blurry circle. This is called the circle of confusion.
If you look at the same thing using a smaller aperture, you can see that the lines still don't come together (we're still out of focus), but they get much closer. Because the lens opening is smaller, the angles of the light rays are smaller and they don't spread out as much. The result is that the blurry area that represents the eye is smaller and so it is less out of focus.
That's it in a nutshell. Smaller openings don't give the light as much room to spread out, so more of it is in focus. The same principle works for squinting (your eyelids act as a smaller aperture) and pinhole cameras (the tiny hole is like a tiny aperture).
While smaller apertures mean that you'll have more depth of field, they don't necessarily mean that you have sharper pictures. When your aperture gets smaller, some light rays start to spray in all directions in what is called diffraction. It's very much like covering the end of a hose with your finger and making the water spray everywhere. The smaller your lens opening, the more diffraction you will get and the softer your image will be, even the in focus parts of your image.
When your lens is wide open, it will have the least diffraction. On the other hand, because the light angles are so large, lots of other problems occur. These problems can result in shapes being distorted (straight lines that bend), colors being distorted (colored fringe in areas going quickly from light to dark), or other nasty stuff. For that reason, you'll get your best image quality somewhere between the aperture extremes. F/8 to F/11 is usually the sweet spot.
Just because your image quality is highest in the middle, don't think that is the way to make the best images. It is often better to trade some absolute sharpness for more depth of field or some distortion for less depth of field.