Paul McCartney wrote this. His friend Alistair Taylor, who was visiting McCartney, asked Paul one day how he wrote his many songs, and how he came up with his ideas. Paul took him into his dining room to give him a demonstration of his hand-carved harmonium. As an experiment, Paul asked Taylor to shout out the opposite of whatever he sang, such as black and white, yes and no, hello and goodbye, etc. From this, the song was born.
The ending where all The Beatles sing, "Hela, hey, aloha," was improvised in the studio.