The other answers incorrectly associate the blur effect with some lens properties. You don't have to assume anything about how the image is formed by the lens or even that a lens exists.
The scene simply looks slightly different from different locations across the aperture.
As you can see in the picture, if you choose to keep the red object in the same position for each aperture point, there is no way the green object can stay in the same position. This creates blur, because the final image combines all those individual views.
This means that theoretically (and ignoring diffraction) the only case when where everything can be in focus is pinhole, creating the image from a single point. In the real life a small but not pointlike aperture is better, because of diffraction and increased amount of light, but that's another question.
Pursuing the subject further, "who" actually selects what is in focus?
Why the red object and not the green one? The geometry only determines that they cannot be both in focus and the amount of defocus depends on the aperture and this is the fundamental reason of the DOF effect.
How actually the final image is combined from partial views? This depends on the "blue box" device. In the real life, the "blue box" is of course lens. Until now, we pretended we don't know anything about how the image is combined in order to show that the out-of-focus phenomenon emerges from geometry and not from the lens properties.
But it does not have to be lens. Instead, we might place thousands of pinhole image recorders across the aperture surface and acquire thousands of individual images. Then, by simply overlaying those images we get the same DOF effect - depending purely on the aperture. And unlike lens, we might then overlay the same images differently, keeping the green object stationary (which would blur the red one, obviously).