Suppose someone is pursuing PhD in mathematics from a third class university and their supervisor has little knowledge about their own field. Is it possible to publish a good research paper in good journal?
Yes, it is possible. The institution doesn't matter. The advisor doesn't matter. What matters is the content (and correctness) of the paper, along with a judgement about its "novelty". Those judgements will be made by reviewers and editors, independent of where the paper originates.
Good writing helps, of course.
TLDR: It is possible, but in practice it's much easier to get the same paper published with a 'Big Name' university in the affiliations. Coming from a 'Third Rate' university, you will need a glowing cover letter to convince the editor to send your paper into review at all.
What has been said so far is true in principle, but not necessarily in practice. Since the OP explicitly asked about the possiblity, the other answers are technically correct but disregard the mechanics of the publishing process: Not all papers get into the review stage. Before being sent for review (i.e. before the scientific importance even has a chance of being assessed by actual experts), the top journals have an editor vet submissions before sending them out. And by 'top journal', I'm not talking only Science and Nature. Nowadays, even journals with mid-single digit impact factors have desk rejections.
This editor has little time for each submission and also is likely to get into trouble if they send too many 'duds' (papers of poor quality that subsequently rejected by the reviewers) into review, because it can be very hard to find reviewers. If there is a 'Big Name' associated with the submission, there is very little risk for the editor in forwarding your paper into review because if it is a dud, the 'Big Name' university provides protection ("I did have my doubts about this paper, but since it came from 'Big Name', I thought it might be worth giving it a shot.").
If you think you have made a significant scientific breakthrough in your paper, then you may have a chance with a sufficiently strong cover letter to your submission. Make it clear what the contribution is and why it is very important. Don't make it "Dear Editor, please find attached my submission. Sincerely yours,...", but make it "Dear Editor, in the attached paper, I've partially solved longstanding problem X / I have shown that method Y can be extended by Z, making problem X finally accessible" or the likes. The cover letter is a sales pitch. Big Name universities are a brand, and if you do not have such a brand attached to your name (yet), you must sell the paper harder.
If - as is the default - you still get your desk rejection, publish in a well-regarded low-tier journal and watch the paper get cited. The next paper has a better chance of getting through the editor.
Another option is Open Acces journals, which can have a very high impact factor and are a bit easier to get into, because your university has to pay a substantial fee. However, beware of predatory journals (https://en.wikipedia.org/wiki/Beall%27s_List).
Yes it does not matter where you come from if the paper is good enough (and you can pay the fees) a journal will publish it.
One issue that could arise, if you/your supervisor is less skilled is being able to turn your good idea into a good paper (so that a good journal would publish it). This needs a good knowledge of the subject area to know how to sell the paper, what are the interesting questions in your field, and how your work fits in and improves your field.
Good papers are not just about having a good idea but being able to sell the idea. This is done though the choice of plots, what you discuss (and what you leave out to stop the paper becoming too long), the literature you cite, and the overall presentation of the paper. I've seen many papers that after reading them many times I eventually realize the authors did good work, they just worded their paper poorly and as such the papers end up in lower tier journals.
If you're worried about the impact (on how "good" your paper is) of not having local access to colleagues with a deep background knowledge of the field, you could try submitting to a journal that has an open, public review stage prior to its formal, traditional peer review, to get access to a pool of expertise. There exist some journals with such a process that can reasonably be described as "good journals".