this has happened to me many times when I shoot weddings and ill tell you the reason why. its a few things. camera and human error. its the size of the sunject youre focusing on. in closeups the focus spot (the eye) has a nice big area to have enough contrast to focus on. when youre using the af on a picture like the second, the area youre focusing on is sourrounded by other things inisde it and if you didnt know the true af area isnt exact to the square you see in the viewfinder. meaning, the little square you see isnt exactly the exact size and placement of the black af area. its sometimes a bit off center then the specified shape. but this is only critical in certain situations.
this is my guess and is based on my experience. the problem in the 2nd picture is the focus slipped between then to the background. im assuming you used single af point and used the center one. you put the focus point on their faces but the area where their faces is so jumbled up with minute contrast areas that the accuracy can be off because the focus point is trying to focus on something there, but since theres so much info thats tiny and not exact it took a guess at the trees behind them. this has happened to me many times, but only when I shoot family formals when there are groups of people. the focus is much better when the area youre using your af spot on is big compared to the focus. the first one, the af has a nice large eye to focus on. the second, their heads and the baby and the trees are all jumbled up together. thats why when I shoot groups of people, I shoot, refocus, shoot, refocus, shoot refocus. and I focus on a different person each time. that way im certain to get a nice picture. they always ask me why are you moving the camera all over the place to take a picture? because I focus, hold the shutter half pressed, recompose and then shoot.
so the answer is you not being accurate with where you placed your focus and the camera because it has certain limits and when there are a lot of minute contrast points together its going to get confused.
btw, this is also true of the other end of the focus scale. when the sunject has too big of a focus area and the focus is off by a tiny margine. when I shoot family formals, I will shoot each and every family member by himself as well. full body, half body, then face, once in a lanscrape orientations and once in portrait orientation. but I have noticed the my focus point is sometimes off just slightly because I wasnt accruact on focusing the eye because the eye is bigger than the af spot in the camera. so my being slightly innacurate makes the camera focus more towards the nose or ear in the focus plane (if you know what im talking about)
its not the camera only. the camera can only do so much but it has limitations. you need to know its limitations and work around it. if you would have done a few shots with focus on the man woman and the baby, you would have had a properly focused shot.