How does a diaphragm affect depth of field for an imaging device?

This has to do with the behavior of the lenses in your imaging device. First of all

Am I understanding this right? As the diameter decreases, objects farther away appear sharper?

This is true but it isn't the whole picture. More depth of field doesn't just make things farther away look sharper, it makes everything not on the focal plane look sharper. When you focus a camera, objects at a specific distance from the lens are in focus. Anything closer or farther will not be in focus. However, things that are just slightly out of focus still appear sharp to the human eye. If you have a shallow depth of field, objects get very blurry when they are a small distance from the focal plane. With more depth of field there is more room for error, objects that are not precisely in focus are still close enough that we perceive them as focused.

What causes this effect?

Mostly spherical aberrations. The best type of lens (though there can be no perfect lens) would be a hyperboloid, which would focus all rays from a point source to another point. However, lenses have spherical faces because they are significantly easier to manufacture than hyperbolic lenses, but this means that as you get further from the center of the lens rays are not focused exactly as they should be, and objects do not focus to a point but to a sphere of confusion. To a first approximation the spherical lens acts ideally, meaning that the very center of the lens (which is exposed by a small aperture) focuses very sharply, but as the diaphragm is opened more of the lens is exposed, so these aberrations are worse. For objects that are precisely on the focal plane of the camera, the aberrations aren't enough to make the image blurry even with a wide aperture, but spherical aberrations have a more pronounced effect on objects that are not in focus, as you can see in this diagram which is on the wikipedia page for depth of field.

/r/askscience Thread