Good question and you've asked the right man.
When beer has finished its primary fermentation in bulk there remains in it a low level of yeast which is carried through to the cask. Residual, or additional, fermentable sugars are gently converted into alcohol & carbon dioxide over the next two or three days (conditioning or secondary fermentation) by this yeast which uses up any oxygen in the cask.
Thus the beer remains 'alive' and if crafted well, safe from spoilage. The flavour will develop over a period of time dependent upon the style and strength brewed. A light ale, for instance, would be at its 'peak condition' within a week and ought not to deteriorate for a couple of months. A true India Pale Ale, on the other hand, may not reach its best for 6 months and can last very soundly for a couple of years or more.
The process is exactly the same in bottle conditioned ale.
As you'll appreciate, such a fresh, live product needs tending properly and that includes storage temperature; too warm and the whole aging process accelerates unacceptably.
As the beer is drawn from the cask it is replaced with air and all the attendant spoilage potential of oxygen and air borne organisms. Thus the cask ought to be finished within three days of opening and the surrounding environment must be clean, as well as cool.
Looking after cask ale doesn't require a degree in rocket science but it does require care and attention, attributes you'd expect from the host of your local public house.