Do we have to write research papers after deciding on the journal we are to publish or do research on our idea and then find a suitable journal for it?
Either works actually. If you already have a draft of some article/review essay, then look out for journals aligning with the theme and type of submission as well (as not all journals accept every type of submissions). If not, then look for journals that match your research interest and check out if they have a "call for papers" and what themes are specified, and you can write your article accordingly.
I generally start research with some idea of where I'm going to publish it, but don't make the final decision until I'm writing up the results. Keep in mind that you might have to submit to a few different journals before you find one that accepts your manuscript.