An API, or Application Programming Interface, is a communication protocol between two sets of code. It's like a language; both parties agree that words have a particular meaning, so that they can talk to each other.
Why? Because Software is Hard.
Hardware makers do their best not to agree on how hardware should be made. Power requirements & connections have to be standardized, but how do you think they could actually make such a great XYZ5000 if they actually told you how they did it?
Originally - especially when a new computer comes out - people wrote code to talk directly to the hardware. MS-DOS games routinely wrote directly into the video memory for fastest results. When you start having multiple programs running simultaneously, they all can't do that, any more than everyone at a meeting can all talk simultaneously, so eventually APIs and driver programs would do the work, and programs would call them in order to play nice with everyone else. This lack of standards meant that there were times that games would refuse to run on certain PCs, depending on their configuration. Many DOS game manuals in the '80s and '90s would devote considerable space to troubleshooting advice, which often meant a lot of tedious editing of configuration files.
A driver implements a particular API that the operating system requires for interfacing with a certain kind of hardware. So hardware makers write 'software drivers' that allow your lowly computer to understand & communicate with the awesomeness that is their XYZ5000, at least on a basic level.
However, Microsoft, Apple, or the various makers of the various *nix's are not stupid; they know there's malicious (or just poorly written) code out there. They are not just going to let any old program willy talk to drivers. Especially drivers relating to drawing stuff on the screen. The OS needs to know about this sort of thing, and it needs to manage access to that hardware.
So the OS exposes an API that allows other programs to talk to the hardware.
Application Programming Interfaces can also refer to the way a developer can make his software talk to another pre-existing piece of code, most commonly preexisting shared libraries. Shared libraries allows a developer to save time because instead of trying to reinvent the wheel, he/she could just invoke the code of another piece of software that does what he/she desires to perform the needed magic on the data, saving time. However, while all libraries have an API, whether the documentation exists or not and the library's licensing can play a role on whether it can be freely reused on another piece of software.