I was outbid at the last second. How can I prevent this?
The only way to protect yourself from being outbid at the last second (also called being "sniped") is to bid the highest maximum you are willing to pay. Remember, it's not the last bid that wins, it's the highest.
There is a common misconception that snipers always win. This isn't so. To win, they must outbid you. If someone places a last-second bid that isn't high enough, they almost never have enough time to try again and place a winning bid before the listing ends.