I think this is covered well by Ken Rockwell. http://www.kenrockwell.com/tech/focus.htm

He argues that for any given scene/camera arrangement there will be an optimum aperture when the effects of depth of field blur and diffraction blur are equal. Deviating from this aperture will mean increasing one effect while decreasing the other. Since the sharpness of the image will be limited by whichever effect is greater, you can't do better than when they are equal.

Ken explains it much better than me. More importantly he goes on to show how to determine it. His formula is fairly theoretical, but it seems to give me good real world results.