My paper was rejected following a lengthy delay during which an editor published a similar paper I have had a very bad experience with a journal. After nine months under review, the system showed that a decision had been made; I have checked and was “revise and resubmit”. Two days later, the editor contacted me to let me know that the paper has been rejected. 

I don’t how to proceed with that journal and felt really disappointed and frustrated especially after it took such long time to respond. I also discovered that one member of the journal's editorial board has published a similar paper in the following issue. 

Any advice on how to handle such a situation?
I would suggest submitting your paper to a different journal, but as a counter to the charge that "it's already published in journal X," include the correspondence you had with journal X. 

This should alert a responsible editor to the fact that something wasn't right at the other journal, and may be willing to give your paper a fair shake.

I'd also consider lodging a complaint with the publisher of the journal. This sounds like an ethical lapse somewhere: another paper got published, but yours was out of scope?