40ma is the absolute maximum rating, NOT a built-in limit. You'd probably get more than that which could fry the Arduino or the LED.
Sometimes people connect an LED to the Arduino without a resistor and it "works" because the Arduino has limited current capability. But it's a "bad design" the Arduino will probably overheat.
LEDs make things a little tricky but Ohm's Law which defines the relationship between voltage, resistance, and current. (Ohm's Law is the 1st thing you learn when you take an electronics class.)
Then you should also understand that current "flows" like water flows through pipes.(1) This means that if you have two resistors in series, or a resistor and LED in series, the same current flows through both components.
Resistance is "the resistance to current flow". Usually the voltage is fixed (or "controlled") so the current depends on the resistance (until you get too much current and something blows-up).
And you probably need to know that resistances in series sum-up. Two 1K resistors in series make 2K so if you apply 5V you get 2.5mA.
And you get a voltage divider where the voltage is divided proportionally to the resistance. If you have two equal resistors half of the voltage ends-up across each resistor. In the above example with the 2.5mA through the 1K resistors, Ohm's Law tells you that there is 2.5V across each resistor.
...Everything comes-back to Ohm's Law. It seems like my old high school electronics teacher used to answer every question with, "It's just Ohm's Law!"
There are some other "Laws" too but most depend-on starting with Ohm's Law.
LEDs are non-liner (like all diodes). Their resistance changes with voltage. As the voltage across the LED goes-up their resistance goes down. When the voltage goes-up the resistance drops by a lot, and you can get excess current and blow-up the LED or something else. And at very-low voltages the resistance is so high that almost no current flows (and the LED will be very-dim or it won't light-up at all).
If you put a resistor in series and calculate it correctly using Ohm's Law, the voltage across the LED "magically falls-into place" and everything is perfect!
Ohm's Law is a law of nature and it's always true. But it's not useful on the LED itself because the resistance varies with voltage. So we use the voltage across the resistor and the desired through it to calculate the required resistor value. (Ohm's Law also gets more complicated in AC circuits where the voltage & current can be out-of-phase.)
High power LEDs (1W or more) normally use a constant current source instead of relying on a resistor. The resistor wastes power (usually as much as the LED) which is not a big deal with regular little LEDs but with high power LEDs you need a high-power resistor and you're generating twice the heat and wasting half of the energy which means you might need a bigger power supply, etc.
(1) In the plumbing analogy voltage is water pressure and resistance is a skinny pipe (or a valve) that reduces current flow.
But the big difference is, if you cut a wire you get infinite current and no water flow. If you cut a pipe you get zero resistance and water flows-out all over the place.
Also infinite water-resistance or zero water-resistance doesn't cause any problems (as long as there's somewhere for the water to go... You don't want a flood!
With zero electrical resistance (AKA a "short circuit") you can get excess current and bad things happens can. Nothing bad happens with infinite resistance (AKA an "open circuit").