fIf you have travelled to Phi Phi Islands can you advise if I really need to book a boat ahead or if I can just go to the pier and buy a ticket for the next boat? (the latter gives me more flexibility if the boats are not usually booked out).
You can certainly book on very short notice, but that there is the obvious risk of everything being sold out. This being said, there is good availability for tomorrow (12/22) so there seems to be good supply at the moment.
Personally I would recommend booking online the day before (for example through AirBnb experience, no endorsement intended). The boat tours vary quite a bit in terms of scope, duration, price, services offered, departure times, etc. If you compare online you can choose what suites you best.
When you book online, you can pay directly online and they tell you when and where to show up. An online booking may also include shuttle service. Once you show up at the Ferry terminal, someone from the boat/company will greet you, check your reservation, and sort you out.
I'm also not sure if and how tour operators sell tickets "directly at the pier". Last time we did this, I didn't see an obvious place to buy tickets directly in the terminal where we were, but that doesn't mean it doesn't exist.