First up, commercial pilots (both major airlines and cargo airlines): It depends on the country and airline, but the retirement age for a pilot is generally 65 in most countries, and almost always somewhere between 55 (e.g. British Airways) and 65 (standard in much of the U.S.)
There is no set last age at which you could be recruited, so in theory it's just before those ages, but in practice you're unlikely to be hired by a company if you can't work there for at least 5 years.
If you're not yet a qualified pilot, though, you'd have to account for this time too (no company is going to hire you and train you just in time to retire). In practice, therefore, the likely realistic age is at least 10 years prior to the retirement age for the country/company in question, and likely 15.
And if we move into pure conjecture for a moment, I'd hazard that you'd find it quite hard to find a company willing to take on an untrained pilot much after their early-mid 40s.
As for a private pilot: As long as you can pass the medical you can fly and, indeed, learn to fly, at any age. There's no maximum age, although as you'd expect most (but certainly not all) people start to struggle with passing the medical sometime in their 70s. By 80, there aren't too many pilots still flying around.