Well, there's two different answers here because those two people came from very different eras.
First, someone can become so famous that they're known by just their first name. Such is the case with Napoleon. Same goes for people today if their first name isn't very common. If you said "West", "O'Neal", or "Barr", you might not know who I'm talking about but "Kanye", "Shaq", or "Roseanne" are immediately recognizable. We typically call famous political leaders by their surname, but there's nothing stopping us from referring to Obama as simply "Barack", for example.
So in Napoleon's case, as the other commenter mentioned, he was referred to as "Bonaparte" until he crowned himself king and thus, "Napoleon I" though even before that, there hadn't really been a famous Napoleon before so "Napoleon" was more than enough, and the convention has stuck.
Now, the other part that probably better answers your question. (This is confined to European history. The Asian timeline on this topic is different):
Surnames as we know them in Europe and the Americas are a relatively recent invention. There's some evidence they were being used as early as the 900s in Ireland, but they didn't really take off until the Norman invasion of England in 1066. In the aftermath, the upper class noble Normans who stayed in England started using a second name referring to a place back in Normandy, and it became hereditary throughout the generations. "The Domesday Book" from 1079 shows that this was happening among the upper class, but lower class people still went by just a first name.
By the end of the 1300s, the convention had reached all English classes as well as French-speaking people back on the continent, too. Scotland and Ireland adopted surnames universally not long after; the Welsh were a bit of holdouts but they eventually joined the club. By 1500, it's safe to say that if you spoke French, or you lived anywhere in the British Isles, you were using a fixed surname, even if you were the poorest peasant in the land.
In most of the rest of Europe, surnames came later. They caught on in Italy and Spain during the 1500s. In the Netherlands, they didn't happen until 1811 (when the country was occupied by Napoleon who ordered a civil registration). In Germany, it happened piecemeal due to its varied regional history. Bavaria, for example, was using fixed surnames by the 1600s, while Schleswig-Holstein didn't make them mandatory until a final decree in 1822.
The last holdouts against fixed surnames were the Scandinavian countries: Norway, Sweden, and Denmark. They really weren't completely universal until about 1900.
Before the use of fixed surnames, last names worked very differently. (And they still work this way in Iceland today.) Last names, if you had one, were patronyms, i.e., your father's first name. If your name is William and your dad's name is John, you're William Johnson. But your kids' last name will be Williamson. Two different last names in the same household.
The problem local populations ran into with this custom is that there would be several people in the same area with the same first and last name. So people were assigned a second last name called a "cognomen". So there might be a "William Johnson the White" and "William Johnson at the Woods" in the same town. Your children would typically be assigned your cognomen in their early adulthood, but would routinely be given a new one if a more suitable description arose. For example "William Johnson Atwood"'s son "David Williamson Atwood" might become "David Williamson Hill" once he got land of his own and moved away from the woods or "David Williamson the Smith" if he became a blacksmith, etc.
So why am I telling you all this? Because unless you were English or French before about 1500, you didn't have a true fixed surname like we use today. In your example, Hannibal's last name "Barca" isn't a true fixed surname. It's a cognomen that meant "lightning". Because they were a prominent family, his sons were tagged with the same cognomen, but it changed after just a couple generations.
All these historical Europeans are thus usually referred to by their first name ("Hannibal") or first name plus cognomen ("Alexander the Great", "Edward the Confessor", "Ivan the Terrible"), because this is typically how they would have been referred to in their time, and it has stuck with them throughout history.
In the case of "Alexander the Great" and "Edward the Confessor", both living pre-1066, they simply didn't have any patronymic or "fixed surname" style last name. First name and cognomen only.
By the time of "Ivan the Terrible", he did have a patronymic last name ("Vasilyevich" -- he was son of Vasiliy III) though his "cognomen" was more useful in setting him apart from his predecessors. His grandfather, Ivan III of Russia, was also "Ivan Vasilyevich" at birth, so by using first name plus patronymic last name just becomes confusing as to which "Ivan Vasilyevich" you're referring to.
And this extends to non-poltical figures as well. "Da Vinci" in Leonardo's name is a cognomen meaning "of Vinci". His full name is "Leonardo di ser Piero da Vinci" meaning "Leonardo, son of Piero, of Vinci". We could call him "Leonardo di ser Piero", and his friends and neighbors assuredly did. But for the rest of Italy, where many other towns had a "Leonardo di ser Piero" of their own, it was much more useful in calling him "Leonardo da Vinci", which, by extension, is how he came to be known throughout Europe. "Not Leonardo, Piero's son, who lives down the street. Leonardo of Vinci."
TL;DR: Fixed surnames as we know them today didn't become universal in most of Europe until the 1600s, and some places in northern Europe until the 1800s. And Iceland still hasn't adopted them. Before that, surnames were ever-shifting patronyms and/or a person-specific cognomen. Patronymics aren't particularly useful outside of a person's local community, so these historical figures are typically referred to by first name only, or first name plus cognomen.
EDIT: For sources, here is one and here is another that give a decent rundown on the history of surnames and their use in Europe. Sorry I couldn't find a more concise timeline.
EDIT: Thanks for the input. I took out the part about Caesar because it didn't add to the explanation.
Napoleon was widely known as Bonaparte until he had himself crowned emperor under the name Napoleon I. So in that particular instance it was Napoleon himself who asked the world to call him by his first name.
One very useful reason would be if other members of the figure's family are also historically significant.
Take Hannibal's case, Hannibal is a member of the Barcid family, which is a very prominent and powerful family during the Punic Wars. His father, Hamilcar, was a general during the First Punic War, his brother, Hasdrubal, was a general alongside Hannibal. Referring to Hannibal as "Barca" would cause confusion and the reader would heavily rely on context to tell which "Barca" was being referred to.
There's a linguistics reason as well depending on culture. Some cultures have a large amount of personal names, others have larger amounts of family names.
English has more distinguishable family names than personal names. Calling Michael Faraday "Michael" would be useless because there are many other significant figures with "Michael" as their first name. When referring to a "George" in 1776, is it George Washington or George William Fredrick (King George III). Both very significant figures pertaining to the same event.
The convention is flipped when you talk about a culture with many similar last names. Nobody in Chinese history is just referred to as "Liu" or "Zhang", because there are millions of them, thousands historically significant. Chinese historical figures are referred to as "last name-first name" or "last name-courtesy name" to allow them to be distinguished.