As the title suggests, how does an automatic gearbox know when to change gear?

For example, how does the autobox know to shift down when you hit a gradient? How does it know to shift up at 2000 rpm when driving gently? When driving 'enthusiastically' how does it know to hold the shift until redline?

On a modern vehicle this will be computer controlled.  In addition to the above, how did older analogue transmissions accomplish this before computer controls?