It is not explained in the books why Gilderoy Lockhart took up the post of Defense Against Dark Arts. But this question can be answered based on J.K. Rowling's interview and information from pottermore.
J.K. Rowling on Gilderoy Lockhart's childhood and upbringing.
So, Gilderoy Lockart was the only child in his family who showed any magical abilities, so he was very proud of himself in going off to Hogwarts. And he was Sorted into Ravenclaw House, though I suspect - I think people will be unsurprised to hear that I think - he narrowly escaped Slytherin. But he scraped into Ravenclaw.
Gilderoy is quite a modern kind of celebrity in the way that he likes to think of new ways to make himself famous. So he decided he’d quite like to put a range of hair-care products out onto the market; he's very proud of his naturally wavy hair. So, I like to imagine that he discovered the luster giving properties of occamy eggs, and occamy eggs come from quite a vicious creature - the eggs have silver shells. So, these shampoos were found to be too dangerous and too expensive to produce, so Gilderoy never achieved that ambition.
Gilderoy Lockhart isn't his pseudonym. Gilderoy Lockhart is his name. I think that that says something about his mother, who was very ambitious for her son, and encouraged him in the belief that he was a remarkable person. Gilderoy's quite a flashy name, I think.
Gilderoy Lockart was an overconfident, vain and celebrity-hungry man. It might have appealed to him to take up the job when other wizards were scared to take it up.
Secondly an opportunity to teach Harry Potter might have been deciding factor in Lockart (similar to Horace Slughorn) taking up the post of Defence Against the Dark Arts teacher.
Albus Dumbledore’s plans, however, ran deep. He happened to have known two of the wizards for whose life’s work Gilderoy Lockhart had taken credit, and was one of the only people in the world who thought he knew what Lockhart was up to. Dumbledore was convinced that Lockhart needed only to be put back into an ordinary school setting to be revealed as a charlatan and a fraud. Professor McGonagall, who had never liked Lockhart, asked Dumbledore what he thought students would learn from such a vain, celebrity-hungry man. Dumbledore replied that ‘there is plenty to be learned even from a bad teacher: what not to do, how not to be’.
Lockhart might not have been keen to return to Hogwarts, given how well his career of stolen glory was progressing, had Dumbledore not dangled the promise of Harry Potter over his fame-hungry head (a ruse that Dumbledore was to repeat four years later, when another teacher needed to be persuaded to come back to school). By subtly suggesting that teaching Harry Potter would set the seal on Lockhart’s fame, Dumbledore had set a lure that Lockhart could not resist.