The junior novelisation confirms that it's "retractable" (as opposed to being manually attached and then discarded).
Quite how this is actually accomplished isn't immediately obvious but I'd assume that it collapses into a smaller size and then slots into the body of the X-Wing somewhere near the top.
Yoda answered, “If you honor what they fight for … yes!”
Luke reached for the lower rung of the X-wing’s retractable ladder and
looked away from Ben and Yoda.
Empire Strikes Back - Junior Novelisation
Interestingly, a deleted scene from Return of the Jedi has Luke climbing a similar ladder and then simply discarding it. No explanation is offered for this discrepancy.
Pablo Hidalgo addressed this conundrum in Star Wars Insider #60, although he did stress that his answer was not part of the accepted canon.
Q. What's the deal with the ladder to Luke's X-wing? In a New Hope, we see his boarding ladder being removed by a tech prior to take-off. When he gets to Dagobah in Empire, a ladder mysteriously and conveniently appears so he can board, but once he's in the cockpit, it's gone. Where did it come from? Where did it go? If X-wings have some sort of automatic retractable ladders built-in, than why does the guy at the Massassi base even bother?
PH: It's a valid question, and there hasn't yet been a source published that's supplied a “real” answer. This response is just a stab at possible solutions and is not meant to settle any arguments. Anyway, no technical manuals point the an X-wing having a retractable ladder, so that's out. Incom representatives no doubt strongly suggest that you land your X-wing only at well-stocked facilities that provide you with a prompt and courteous ladder service.
But what if you land on some forsaken planet like Dagobah? What then?
X-wing fighters have cargo holds in their bellies that can hold 110
kilograms of supplies - You can see it clearly in Empire. A pilot can
access the hold by removing a section of the cockpit seat - Presumably
that's how Luke got all those boxes and gear out of his ship when it
was still in the Dagobah lagoon.
So, that ladder is usually kept there for pilots who must put down in
remote locations, though getting it out of the hold and draping it on
the side of a fighter must be awkward, unless it's hinged or
something.
The odd part about Empire is that Luke never removes his ladder.
Hopefully that was his intent, and the ladder is designed to fall off
and litter the Dagobah countryside. Otherwise we're left with the
unfortunate conclusion that Luke did the Star Wars equivalent of
driving away with his coffee cup still on the roof of his car.