Writing your farewell speech can seem like a daunting task, but what you need to keep in mind is that you do not have to say goodbye to each person personally in this speech. There is a good chance that you will speak to many people after you give your speech, and moreover, you may stay in contact with many of them.
A farewell speech is simply a summation of what you think about the time spent working together. It should include a thoughtful thanks for the event at which you are speaking (such as a farewell party), an anecdote, and your thoughts about your time spent together.
If you are, for instance, retiring from your job, you might say something about how proud you are to have worked for this company. If you are saying farewell at a party because you are moving away, you might mention that you only hope you find equally devoted friends where you are moving. It does not have to be a masterpiece sentence; it only has to close your speech.
For your anecdote, keep it light. Recall a funny story that is general enough to be enjoyed by everyone present. Inside jokes and inappropriate stories are absolutely out. Additionally, remember to keep your story short. A long speech will bring down your party.
Finally, don't worry too much about your farewell speech. No one expects you to be a seasoned public speaker. Focus on making your personal goodbyes meaningful, and enjoy the time you have left.