Was this by nomination of a member of royalty?
Obviously these titles are inherited but how did they get there in the first place?
So, let's start with some background information. All peerages in Britain are/were created by the reigning monarch. The British peerage system has 5 ranks. They are, in descending order from highest rank to lowest: Duke, Marquess, Earl, Viscount, Baron. There is a 6th hereditary rank, "Baronet", but baronetcies are not technically peerages. They are instead hereditary knighthoods, and baronets, while enjoying social prestige and with possibly as much wealth as peers, did/do not hold the same social rank as a peer in terms of precedence etc. The peerage system present in the British Isles has several different levels and geographic designations, depending on when and where a particular title was created. For example, there is a Peerage of Ireland comprised of the peers created by the British monarch as sovereign of Ireland. Creation of titles in the Peerage of Ireland ceased in the late 1800's, but many are still extant. There are also some very old extant Irish peerages that predate the British conquest. There is also a peerage of Scotland.
The current Peerage of the United Kingdom came into being in 1801, after the Acts of Union of 1800 merged the Kingdom of Great Britain and the Kingdom of Ireland. The Peerage of the United Kingdom replaced the Peerage of Great Britain, which had itself replaced the Peerage of England and the Peerage of Scotland when those two countries passed the Acts of Union of 1707. The Peerage of England and the Peerage of Scotland both stretch back very far into history, nearly to the Norman Conquest of 1066 in the case of some Peerages in England in particular. The Earldom of Arundel was created in 1138, and is still extant as a subsidiary title of the Dukedom of Norfolk (that is, the Duke of Norfolk also holds the title of Earl of Arundel, and Earl of Arundel and Surrey, a second merged title, is used as a courtesy title by the sitting Duke of Norfolk's heir). When a peerage was created has importance in determining a peer's order of precedence. It isn't uncommon for peers, especially higher-ranking and older peerages, to hold multiple titles that were not all created at once. This is because the aristocracy over the centuries has seen things like families merging through marriage, individuals dying and the title go to a distant cousin who is already a peer, the revival of dormant historical titles by a monarch for a living family member.
Creation of hereditary peerages (or elevation of individuals who were already peers to higher-level peerages) as a reward for service to the country in some form (military, diplomatic, economic, serving in some role in the royal household or in the government) was a regular thing until the early 1960's, when hereditary peerages as reward was replaced by the creation of life peerages. That is, someone is created a baron for life, but the title does not pass to that person's descendants. Since 1964 only 8 people have been granted hereditary peerages, and four of those individuals were high-ranking members of the royal family (Princes William, Harry, Andrew and Edward). Of the other four (one baron, two viscounts and an earl), all were created for high-ranking government officials. The last non-royal high ranking peer created was when former Prime Minister Harold Stockton was made hereditary Earl of Stockton in 1984. A reigning monarch could revive the creation of hereditary peers if she/he so chose. Though it might spark a public backlash in terms of going against the idea of an equal society etc. there is no express law against it. The social and political importance of hereditary peers has absolutely declined since the late 1800's, but many peers still hold considerable wealth, lands and influence in British society. The current Duke of Westminster, for example, owns large amounts of London real estate and has an estimated net worth of ~$13 billion USD. Many peers no longer have significant wealth, lands, country homes, etc. and just live normal lives. Some do not use their titles, and some peerages are dormant (meaning they still technically exist, but no one has claimed them).
The direct and simple answer to your question of how could someone gain one of these titles is "do something to attract the favor of the reigning monarch, or someone with their ear (such as a Prime Minister, Chancellor of the Exchequer, military commander, even a favored lady-in-waiting)". This was often much easier for someone who was already part of the system. While historically there are "rags to riches" sort of stories, where individuals from poor or modest backgrounds rise to a peerage through courage, pluck, hard work, etc., much more common were for second (or third, or fourth, or fifth) sons of existing peers striking out and gaining the attention of the country via military exploit or the like, or sons of landed or well-off but non-noble families doing similar. This group of people already had means and access to things like military rank (or at least access to opportunities, if not means).
Perhaps a good example of someone who rose quite meteorically to a high-ranking peerage (though not from an especially modest background) was John Churchill, who eventually became Duke of Marlborough. John was not born poor, however. His father was a knight, landowner and famous military officer. John's sister, Arabella, became lady in waiting to Anne, Duchess of York. Her husband, James, Duke of York (the future James II) took an "interest" in Arabella, and this sexual affair (which produced four children, including two sons who were created dukes themselves) led to prime job postings for Arabella's brothers (including John). Through a combination of skill and shrewd intelligence Marlborough rose through the military ranks and after helping put down a rebellion in Scotland was in 1685 made a major general and created Baron Churchill. By 1689 he was created an Earl, Earl of Marlborough. In 1702, in recognition for his military service and loyalty, he was made Duke of Marlborough and Marquess of Blandford. But there is slightly more to that story. Marlborough had fallen out of favor with King William III, but in 1701 William died and his daughter Anne became Queen in her own right. The future Duke's wife, Sarah, was very close with Anne. Thus, Anne's rise to the throne brought a wave of honors and wealth to the Marlboroughs, including their Dukedom. Some of the relationship between Sarah and Anne is depicted in popular culture in the recent film "The Favourite". So it sometimes did happen that people somewhat outside the circle could rise quite high within it through various means, but it's also important to remember that John and Sarah didn't exactly start at the bottom rung either. Sarah's father was also connected, as her father was a Member of Parliament, a position that allowed her to move in high ranking circles and learn the "rules of the game", so to speak, very early in life. Not coincidentally, Sarah's sister Frances was a Lady-in-Waiting to the Duchess of York. And so with each bit of information it becomes clearer that the Marlboroughs were not a "poor boy makes good", Andrew Carnegie-esque story... these were sharp, shrewd, well-connected people who knew how to play the game and did it very well, gaining vast wealth, lands, palaces and titles from their association with the royal family.
So attracting the attention of the monarch in a positive way (or someone in their circle with influence, who could talk to the monarch) was the way to attain a hereditary peerage. While there was no one single way to do this, being born "fortunate" to start with certainly helped. That is, the younger sons of a marquess (for example) likely had a better shot at one day gaining a peerage than someone born poor, simply because they had many more opportunities to end up in the public eye. Their father would have been able to do things like purchase them a military commission (leading to opportunities to shine in that way), use influence to gain them a diplomatic posting, provide them with the money that would allow them to move in top circles where they would get noticed for other opportunities, set them up in opportune marriages with other wealthy, titled families... all opportunities a poor person wouldn't typically have.
Sources:
"The Decline and Fall of the British Aristocracy" by David Cannadine
"The Marlboroughs: John and Sarah Churchill (1650–1744)" by Christopher Hibbert