There are planets known as rogue planets which are not in orbit around a star, but instead wander through interstellar space. Some of these may have formed on their own (i.e. never been in orbit around a star) but most were probably ejected from a star system.
This is not the only way that a planet can end up around another star. Stars move independently to each other. Sometimes they will come close to another star. It is possible for a distant planet to be ripped from it's orbit and end up orbiting the other star.