When being wai-ed to, you would be considered impolite if you didn't return or at least acknowledge the wai - something especially difficult to do when you are carrying a lot of bags or in the middle of stuffing your face when someone sneaks up on you. At the very least, you can grunt and nod. Only monks and royalty do not need to return a wai.
As well as being a greeting, a wai is a show of respect. It can be difficult to determine when you should wai or when someone should wai you. As a foreigner, it's just easiest to wai important people as soon as possible. If someone is going to wai you first, they probably already beat you to it.
However, if you really want to know when to wai, this depends on your relationship to the other person. The person of higher social status will be wai-ed to. You have only a few seconds to size up someone you meet and determine this, but it's best to err on the side of waiing first if you are unsure.
One easy determination is age. If someone is older than you, then you should wai them unless they are someone whom you employ, such as a housekeeper. Additionally, you should never wai to anyone who you are paying for service, such as waiters, tailors, vendors, shopkeepers or taxi drivers. You also wouldn't wai your peers and friends. Alternatively, when leaving a party or other such gathering, you should wai everyone, as this is the polite way to excuse yourself from the festivities.
You will also wai when receiving a gift from a superior, as a sign of thanks. Make your wai before you reach to take the object. While listening to a Buddhist sermon, it is also customary to hold a wai during the entire programme. You may even notice that as drivers of vehicles pass a sacred monument, they will wai - even taking their hands off the wheel!