The quickest fix to this is to half-press the shutter to focus while directly pointing at the subject you most want to focus on (most cameras give emphasis to what's in the middle of the frame), keep it half-pressed, and then move the camera to take the picture you originally wanted, then fully-press the shutter. You can also set the camera to choose a specific focus area instead of choosing one automatically, which will make this technique even more effective (and necessary).
The lens has a limit to how close you can take photos. This is probably only going to be the case if you are extremely close, say less than 12 inches from the subject on a typical lens. Some lenses can focus very close, say 4cm (2 inches). Others designed for viewing distant objects may have trouble below 2 feet.
Normally if this is the case, there will be a light or beeping noise (or different/lack of beeping noise) to indicate the autofocus failed.
- There is not enough light for the autofocus to work well.
In dark environments such as a dimly lit room, the autofocus sensors in a DSLR may not work as well, may work more slowly, or may not work at all. Even in situations where there is enough light to take a photo (say, using a high ISO or using a flash) there still may not be enough light for the autofocus sensors to work well.
Many cameras have an autofocus-assist light (orange or red light coming on during autofocus) which you can enable to help with this in some situations. Again, the camera should show a light, or beep (or not beep/beep differently) to indicate that the autofocus is having problems.