Where I live, I've noticed many buildings have a stone/gravel barrier between the building foundation and the lawn or flower bed. Why is this? Does it prevent insect or pest infiltration of the structure?
You don't want water-retaining soil sitting against the building because this causes damp problems. Creating a porous soakaway like this will mean any water sinks to a lower level than would cause any harm to the building.
Perhaps I missed something in the other answers. The one and only reason is because of SPLASHING. Rain over the gutter, a hard sideways rain will splash mud/soil onto the siding. Not good for siding as the mud tends to hold moisture too long and can cause rot as well as a dirty 'skirt' on your siding.
The gravel, drain rock, cobble or even the lava rock as pictured in the first picture however needs to be below the siding at least 4" otherwise the rock becomes a moisture problem. That first picture with the lava rock and the trap? That lava rock is touching the siding. The siding appears to be concrete. Concrete foundations need to be coated with asphalt for protection. Concrete is very much harmed by moisture. The picture with the 4X6 holding in cobble? If that rock is as close as I think it is to the siding, that will rot the siding if it is not at least 4" below the siding.
Before the cobble or gravel gets installed this is when landscape fabric is necessary. Not to prevent weeds but to prevent the cobble sinking down into the soil and the soil replacing the cobble.
The following is off topic but hopefully helpful...
I wish I could find a more common mulch people could use instead of chunky bark. This is not for weed suppression it is to unify a landscape by covering the soil with a regular texture. Chunky doesn't work. Fine works far better for aesthetics and for decomposition sometime in the near future.
The only mulch I will ever use in the landscape is human poo mixed with sawdust and completely decomposed professionally. Grins. That 4"X6" (not pressure treated)? is a great condominium for insects and other animals; slugs, snails, earwigs, pillbugs...and many more. Not at all necessary or even beneficial.
I lived and breathed construction in all forms for half a century. The only reason and it works well, for using cobble at the base of an outside wall, is for splash.
If this is your lawn here is the best way to fix those edges. Also look up how to produce and maintain a cool season lawn crop! On our site, not on Scott's or Ortho...Use a hose or string, a center to make a set radius to change when changing point of radius to the outside of the lawn. Keep radius curves consistent for each curve. They can change radii every curve but not during each curve. lawn edging the right way
My guesses are that it just looks a lot neater and also prevents weeds from growing near the foundation. If weeds and their roots are allowed to grow there you would have to be constantly digging them out, and roots are already hard enough to get out of the ground without having to dig near or under that area.
Another reason, I think, would be because it is a more permanent solution to weed prevention instead of mulching. Mulching has to be (or should be) removed and/or replenished yearly to maintain an attractive appearance. Lots more work to do that way.
Yet another reason may be to keep water draining away from the foundation. And as you say, reduce the likelihood of pest/vermin infestation, since animals and insects have an easier access to the house through soil and grass. Possibly a combination of all of the above(?)
Some jurisdictions require non combustible mulch i.e. gravel next to the structures. Several buildings have had fires started from discarded smoking materials in combustible mulch.