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.

[![aperture vs depth of field][1]][1]

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.

  [1]: https://i.sstatic.net/3Ip3X.png