What was offensive about the "ladies lingerie department" joke, and how can I avoid offending people in a similar way?
tl;dr: The joke was funny because it was absurd and taboo (made people uncomfortable). It was "offensive" because it may have been intended to make the person operating the elevator feel embarrassed or singled-out. Avoid making jokes that make people feel singled-out.

How is the joke funny?

As other answers have pointed out, the joke is funny because it makes reference to a time when elevator operators were the norm and people would call out the desired floor in a department store by asking for a specific department. It is an absurd (out-of-place) comment to make in the context (the present day, and not in a department store). Often, absurdity is funny. 

There is, however, another layer to the humour. People will often laugh when in an uncomfortable situation. The joke is uncomfortable because of the floor that was asked for: ladies lingerie. In some cultures (I can say Canada and the UK for sure) the topic of undergarments, especially ladies undergarments, is taboo and therefore not supposed to be discussed in "polite society".

In what way was the comment offensive?

I think other answers are close, but slightly miss the mark. The reason I think the joke is offensive is because, whether it was intended or not, it made the person operating the elevator uncomfortable or embarrassed. On a more subtle note the reason is context.

The person operating the elevator may have felt like they were targeted by the joke. They asked an innocent question "What floor?" and the joker replies with the less-than-innocent answer "Ladies lingerie". We can only speculate as to the intentions of the joker, but it is clear that while the joke may have been directed to everyone in the elevator, the answer was directed to the person operating the elevator. Add the fact that the person operating the elevator was a woman, and it doesn't seem so far-fetched to think that she may have felt targeted by the joke.

To further consider this point: why was the department "ladies lingerie" chosen? The joker could have asked for "the hardware department" or "men's clothing" or "sporting goods" and the joke would have drawn a few chuckles. "Ladies lingerie" was chosen because it is taboo, and it is extra taboo because it is a man directing the comment at a woman. More discomfort = more likely to provoke laughs (even though the people in the elevator might feel more uncomfortable than tickled).

Finally, consider the context: a male dominated space. I can say from personal experience (as a man) that it is not uncommon for a group of men to direct lewd or taboo comments at women (or sometimes a young/innocent-seeming man) to provoke embarrassment. Heck, I'm guilty of doing it myself, before I realized how it makes the target of the joke feel! It is likely that the person operating the elevator experienced this sort of behaviour many times before and assumed (perhaps rightly) that the joke was targeted at her and was intended to make her feel embarrassed.

Making someone feel embarrassed for your personal entertainment = bullying. Bullying should not be tolerated, especially in a professional setting.

If it was an elevator full of women and a woman made the joke, the context would be different and the person operating the elevator may not have felt singled-out. Similarly if it was entirely men. However, even in these contexts it is possible for the joke to be construed as bullying, especially if the target of the joke is someone that the joker knows would feel embarrassed.

How do I avoid offending people in a similar way?

Avoid jokes involving topics that make people uncomfortable, especially when telling jokes to strangers or acquaintances. Generally avoid taboo topics of whatever culture you are in and especially avoid jokes that might make someone feel targeted or singled-out based on race, skin colour, sexuality, gender, etc.