In the Grand Theft Auto Online update 'Bikers', the ability to melee other players, enemies, and NPCs (Non-Player Characters) was introduced. There are other tutorials on how to use melee from a motorbike in-game however they tend to omit some key information that will be included in this guide.
Anything aside from throwables that you normally have access to on a bike can be used in melee (yes, even pistols), not just melee weapons as most have stated. Others have also stated that you must be moving slowly on the bike in order to perform the actions. While this will potentially improve the accuracy of your attack, it is not necessary and can be done at any speed.