Why did Shakespeare write Othello?
If you read the entire Shakespeare canon, you become aware that Shakespeare was obsessed with female infidelity. This is dealt with not only in the plays, but also in the sonnets: "When my love swears that she is made of truth/I do believe her, though I know she lies" (Sonnet 138). The image of the "horns" of the cuckold appears over and over again in his work.
Where this obsession came from, we can never really know, although scholars continue to speculate. Perhaps the "Dark Lady" of the sonnets was flagrantly unfaithful? In any case, in Othello, Shakespeare examines how jealousy and suspicion can poison a true love and the consequences of allowing an unfounded obsession to dominate action. Desdemona is a true innocent, and she suffers and dies because of Othello's fears, incited by the sublimely evil Iago.
Shakespeare clearly also wanted to continue to explore the concept of a simply evil man, something he had already dealt with to some degree in Richard III. However, in that play, he can't resist giving the gleefully villainous Richard some motivation (and one famous scene) so that the audience understands, to a degree, the reasons he is the way he is. Iago, on the other hand, spouts only very specious and flimsy reasons for his hatred of Othello. He was passed over for promotion. He "suspects" his wife of infidelity with the Moor, which is completely unbelievable. Ultimately, however, he's just bad and sadistically enjoys seeing the effects of his manipulations.
Then, although the "tragic flaw" theory is not in fashion at the moment, we can't help but see that the story becomes tragic, rather than melodrama, because Othello, a great man, is brought down by the crack in his armor, skillfully targeted by Iago.
Did Shakespeare also intend Othello to explore the concept of racism, in this case also internalized by Othello and causing his initial doubt to metastasize? The Elizabethans were not us, and we export our own views to their world at our peril—but, I believe, that as in The Merchant of Venice, the fact that we see these plays through a different prism merely illustrates why Shakespeare, as Ben Jonson wrote, "was not of an age, but for all time."
Since Shakespeare left behind no personal accounts of his life—no letters, no journals, no essays—we have to use educated guesses as to why he wrote any of his plays. Scholars such as David Bevington believe Shakespeare wrote it near the time of its performance in 1604, when Shakespeare's players performed it at the court of James I. Apparently, if this timing is correct, Shakespeare wanted or needed a new play to perform for the king. He may have come across the story idea in an Italian drama called Cinthio by Giraldi, written in 1565 and translated into French in 1584, which features characters with the same names. The warfare between the Europeans, who were Christian, and the Muslim Ottoman Empire was on people's minds in the sixteenth and early seventeenth centuries, and a play starring a Moor and set in Venice might have seemed enticingly exotic enough for Shakespeare. Shakespeare himself would have been turning 40 in 1604, and it is likely that he too had concerns with entering middle age (and 40 would have felt older then than now) similar to those plaguing Othello, who fears he is too old to hold the affections of his much younger wife.
As always, Shakespeare was a great chronicler of the times. Venice was an important trading post and link between Europe and the East. At the time the play was written, there would have been ambassadors from Venice and beyond visiting Elizabethan England. Shakespeare would have enjoyed studying these 'exotic' strangers. Othello himself is referred to by Shakespeare as 'an extravagent, wheeling stranger' and this comment may have been coloured by Shakespeare's observations. The other answer of course is that, ever the magpie, Shakespeare 'borrowed' the tale by Cinthio called Hecatommithi, but adapted it coloured it according to his own fancy.
Some scholars believe Shakespeare based Othello on a short story called Gli Hecatommithi, which was published in 1565 by Geraldi Cinthio. The story dealt with the unfaithfulness of husbands and wives. Shakespeare might have found this theme excellent material for a stage play.
Shakespeare was probably also aware of accounts of wars between Turkey and Venice in the fifteenth and sixteenth centuries (Othello is general of the Venitian armed forces).
The plot allowed Shakespeare to explore issues of jealousy and deceit in the context which it can be most powerful -- that of a husband and wife.