It's based on seniority. If a flight attendant has a more senior staff number than a captain, then the flight attendant has priority. A pilot may be entitled to business class and a flight attendant might not be, but if there is a business seat left and no economy then the flight attendant will still get on before the pilot. They'll be upgrading a regular passenger instead.