Illegal? I doubt it, in most places. It's purely a courtesy thing, I suspect. Maybe some jurisdictions have rules or regulations, but I'd be astonished to see them enforced. You're more likely to get shouted at by a taxi marshal or the taxi driver for the taxi you join, as they don't like to see their fellow taxi drivers disadvantaged. They might well refuse to take you.
Don't forget that in most very busy places (big airports etc.) there is often a long 'off-site' line you aren't seeing, so even though they look like they've just joined the line, they may well have been waiting hours. You saving them 10 minutes from their wait isn't going to seem like a big deal to them, it'll just look like you're doing something odd.
Having said all that, in most places (for example, London) taxis aren't strictly allowed to refuse a ride, as long as you fulfill all the other standard criteria (e.g. requesting to travel within their "jurisdiction"). If they get disagreeable about it, insist. It might be polite to give a generous tip for a short distance, though.