I think there are two parts to this question: How do we know that the transiting object is a planet, and how do we know that it's gravitationally bound to the parent star, rather than an interloper?
If the object is indeed orbiting a star, we should see periodic transits. If a candidate transit is observed, follow-up observations should also be able to observe it. A lack of further confirmation would be a strike against the idea that the object is indeed bound. An additional tool that's useful here is spectroscopy. If the body is orbiting, it should influence the motion of the parent star, because they'll orbit the shared center of mass. This means the star's motion with respect to an observer will change over time, causing a Doppler shift in its light. This shift can be measured throughout the body's orbit. If a periodic shift is observed - with the same period as the transits - then we have another bit of evidence for an exoplanet.
Now, the light curves of transiting exoplanets can look similar to those of eclipsing binaries - binary stars where the orbital plane of the components is aligned with the line of sight of the observer. A couple key elements can help astronomers differentiate between the two:
- An eclipsing binary should display a secondary eclipse when the less luminous component passes behind the first. This is not likely to be prominent in the case of a transiting exoplanet.
- The mass of the orbiting object can be determined by measuring the radial velocity of the star, as determined by looking at the Doppler shift. This is an easy way to distinguish an exoplanet from a star.
In short, an occultation of a background star by an interloper would not be periodic and would not be associated with Doppler shifts in the other star's spectrum. The mass of the orbiting body - as well as the presence or absence of a significant secondary eclipse - can help us determine its nature.
I'd like to note that we've directly imaged a number of exoplanets over the past decade. Beta Pictoris and HR 8799 are well-known examples. There are some difficulties with this, of course; planets are much less luminous than their parent stars, and so the light from the central star must be blocked during observations. Direct imaging is useful for planets in face-on orbits, where we're looking nearly perpendicular to the orbital plane. These exoplanets will, naturally, never transit their parent stars from our perspective, and so transiting planets aren't usually directly imaged planets. Additionally, direct imaging is also good for planets in orbits with large semi-major axes - which are also planets that are unlikely to yield strong transits from any position.