Choose a good match between supervisor and student
I think that having a good relationship with a supervisor starts with the match between supervisor and student/postdoc. Not every supervisor is a good match for every student, so it is not as simple as "good" supervisors and not.
I think sometimes students make foolish decisions about how they choose their supervisors: they join labs only because they are accepted or because of the reputation of an institution. They join labs without seriously considering other options, without interviewing in person, without meeting their PI, without having outside support systems.
Admissions systems in some fields and in some countries make it worse, and abusive people can be skilled at hiding it, but the advice I always give prospective students is that advisor choice is the most important grad school decision.
Post docs can be a bit more independent, and so they may benefit more from a fancy name or pedigree, but they should still consider what they will get out of a lab and how the mentor/mentee relationship there will fit their goals.
Communicate regularly and honestly
I think a good mentor/mentee relationship means that communication happens freely between the parties. For mentees, it is important to share information with your mentor at all phases of work and especially when there are difficulties. When students try to hide negative results, bad things happen.
As a student, be prepared to deal with (and not fear) criticism and to learn and grow from it rather than be afraid. Because you are learning, you will not be perfect. Expect that, and be prepared to make and learn from your mistakes.
Additionally, it is important to communicate expectations and goals at all phases of work. For every paper/project, figure out up front who is responsible for what, including authorship expectations. Check in with updated schedules as intended goal dates come and go.
Along with my first point, prospective/interviewing students and post docs should be asking current students and staff about the communication style in the lab and get some sense of whether this type of honest communication will be supported or not.
Develop other networks of support
Sometimes, advisors might be too busy to meet all of their mentees' needs even when everything is going well in the mentor/mentee relationship. Students can avoid getting into a situation where they are stuck if they build other networks of support. Many graduate programs encourage this by forming thesis committees early that consist of other professors who can be the start of a broader network, but even if this isn't mandated students should be looking for these opportunities.
Along with my first point about choosing a good match in the first place, students and postdocs should consider these other peripheral support networks when deciding on an institution. Don't get stuck in an institution where only one professor does anything remotely similar to what you want to study. Don't go to an institution where these other support networks are discouraged. Ask people about these things before you go.
These alternative networks will be critical if your relationship with your primary advisor deteriorates, which will sometimes happen even if the mentee does everything "right." It will always be easier to move on earlier on rather than later, when too much has already been invested. Still, it is important to address issues as they come up, before there is too much baggage.
By addressing problems early, it will be more clear whether the situation can be salvaged or not. If you wait to solve problems and try to hide or ignore them, they are unlikely to just go away and you will be stuck deeper.
Yet again, back to my first point: choose a supervisor who works at an institution that will support you if things go wrong. There should be policies in place to support students who have problems with their advisor. Avoid institutions who do not have those sorts of policies in place or that have no support system outside of your one advisor.