When the LED starts conducting, it will allow as much current as the source can supply.
The resistor limits that current to an amount that: will not burn up the LED, and will not damage the current source. Typically that is 20mA max.

So, i f you know the voltage the LED starts conducting, Vf, and you know source voltage, then the rest of the voltage will be across the resistor. Once you know the voltage across the resistor, you can select a value to limit the current using Ohms Law:
Voltage = Current * Resistor, typically shown as V=IR.
A little mathematical manipulation yields V/I = R.
So (Vs - Vf)/I = R
(5V - 2.2V)/.02A = R = 140 ohm with Red LED Vf of 2.2V. 150 is a standard value, yielding current of:
(5V-2.2V)/150ohm = 18.67mA

This analysis is cool, but, it doesn't mention duty cycle.

If we were to use a 50% duty cycle, say in the case of an IR transmitter switching at 36KHz, then, since the LED is off half the time, we could effectively double the power in the LED during the on time.

Since power (watts) is volts * amps, and using the a fore mentioned calculations limiting the average current to 20Ma, then we could recalculate the resistor to allow 40Ma when the LED is on.

(5V - 2.2V) / .04A
or
2.8V / 0.04 = 70 ohms

Standard 5% values have a 75 ohm step, reverse calculating yields:

75 ohms * 2.8 volts =37 Ma.

As far as I know, it might be possible to reduce the duty cycle to at least 30%, maybe 20% (experimentation recommended), if we choose 25%, that would further reduce the resistor to about 36 ohms or 77Ma.

But, you better make sure your software doesn't accidentally leave the LED on for very long, at that current, one second may be enough to let the smoke out. If I were designing it I would use a transistor to power the LED, and feed the transistor with a cap, so in case the software locked up, the transistor would turn off by itself.

Depends on the LED. Some will allow higher pulses for short times:
Peak Forward Current (1/10th duty cycle, 0.1ms pulse width)
Some won't.
Don't generalize, find the spec's for the LED being used.