'Fag' is an abbreviation of 'fag-end'. That was used to mean the last part of anything such as a piece of cloth or, indeed, anything that was a sort of 'flap'. The idea behind it is the fact that many smokers allow the cigarette to dangle from their lips.