If you find that your writing wanders off topic on a semi-regular basis, welcome to random or abstract thinking! The best way to stay focused is to fully deconstruct the prompt before beginning writing. Then, go through the following thought process:
This is the critical thinking algorithm I use on each and every essay I ever write / grade / comment on.
1) What is the most focused question (or set of questions) that this essay is asking me to prove I know and understand?
2) Does the combination of my thesis and my body paragraphs adequately answer the question(s) posed by the prompt?
3) Assuming that the questions have been answered, your thesis/body paragraphs structure is relevant to the prompt, and your writing is free of glaring spelling/usage/grammar/punctuation errors, go read the essay **one more time** and make sure that you have written your transitions in such a way that you've literally bridged the ideas for your readers. In other words, you lead them from idea 1, to idea 2, to idea 3, and then to the conclusion.
4) Now > the scary but critical part: find a quiet spot and read that writing aloud. It is - bar none - the best way to find errors in your own writing.