(This answer is something of a melange of previous answers to different questions that each got at a part of answering this question without quite putting it all together)
Firstly, a distinction: 'rap' isn't a genre, per se; it best describes a vocal style involving a sort of rhythmic talking - so someone can do a rap within a metal song or an indie rock song if they so choose. Instead, 'Hip-hop' is the usual name for the genre you're referring to - and it's not just about the vocal style; I'd be comfortable calling Dr Octogon's album Instrumentalyst (Octagon Beats) hip-hop...and it's instrumental! Hip-hop is fundamentally about an approach to music, of which rapping is (an obvious) part but not necessarily necessary to it being hip-hop - the backing music matters a lot too.
As to where hip-hop came from, like any other genre, there's a variety of things that came into play. But first, let's go back to a (justly famous) house party in the Bronx in 1973 that's widely seen as the 'origin myth' of hip-hop. The DJ at this house party was a young immigrant called Clive Campbell, who had moved to New York from Jamaica as a 12-year-old in 1967.
Campbell had grown up steeped in Jamaican 'sound system' culture. This culture emphasised deep bass sounds, and a certain culture of playing records but doing tricks with it, like putting effects on it and having people talk over it here and there (for more on Jamaican sound system culture - the culture behind reggae and other Jamaican R&B genres - I like the book Bass Culture by Lloyd Bradley). This kind of thing is all run-of-the-mill with DJs today, of course, but was quite novel in New York in 1973. Anyway, at one party put on by Campbell's sister, Campbell found that his partygoers were largely non-Jamaican and weren't interested in his usual Jamaican-flavoured set. Instead, he played the funk and soul the crowd was interested in, but did so using Jamaican sound system techniques they'd never heard before. As a result of the buzz this caused, Campbell became a local sensation under the name of DJ Kool Herc.
To quote Jeff Chang's Can't Stop Won't Stop:
In a technique he called “the Merry-Go-Round,” Herc began to work two copies of the same record, back-cueing a record to the beginning of the break as the other reached the end, extending a five-second breakdown into a five-minute loop of fury, a makeshift version excursion. Before long he had tossed most of the songs, focusing on the breaks alone. His sets drove the dancers from climax to climax on waves of churning drums. “And once they heard that, that was it, wasn’t no turning back,” Herc says. “They always wanted to hear breaks after breaks after breaks after breaks.”
Kool Herc rapidly became a sensation in the community, and imitators sprung up like Grandmaster Flash and Afrika Bambaataa who began to eclipse Kool Herc in popularity, who did what Kool Herc did - spun 'breaks' for people ('b-boys', standing for break-boys) to dance to.
But obviously, rap is also a big part of hip-hop. So when did the MC (the rapper) come into the picture?
In the very early days of hip-hop (1974-1975ish), the MC was essentially functional - an MC at this point was an MC in the same way a wedding might have an MC: to announce things. They were there to make announcements, gee up the crowd a bit, etc. In Joseph C. Ewoodzie Jr's excellent 2017 book Break Beats In The Bronx: Re-discovering Hip-Hop's Early Years he has a quote from Grandmaster Caz about what MCs did:
The microphone was just used for making announcements, like when the next party was gonna be, or people's moms would come to the party looking for them, and you have to announce it on the mic. 'So and so, your mother's lookin' for you at the door'. You know, that kind of thing. So different DJs started embellishing what they were sayin'. Instead of just sayin', 'We'll be at the PAL next week, October this and that,' they say, 'You know next week we gonna be at the PAL where we rock well, and we want to see your face in the place', things like that.
As an aside, it's staggering how young the hip-hop community was in the mid-1970s - many participants had barely gone through puberty; if Grandmaster Caz is talking about 1974 here, well, he was 13 at the time.
Anyway, because hip-hop DJing in the 1970s was a labour-intensive activity - if you're just playing snippets of songs, you don't get much time to stand back from the decks - DJs often opted to have someone else do the MCing; for Kool Herc (generally considered the originator of hip-hop), this was someone called Coke La Rock. Grandmaster Flash - one of Kool Herc's first competitors, along with Afrika Bambaataa - initially let basically anyone come up on stage and MC.
However at one Grandmaster Flash show at Park 63 (which Ewoodzie implies dates to summer 1975), a 14-15 year old dubbed Cowboy (for his bowleggedness) came up on stage to MC and made quite a stir. Ewoodzie quotes Melle Mel (soon also to join Grandmaster Flash as one of the Three MCs at this point) as saying that Cowboy was already rapping "throw your hands in the air and wave them around like you just don't care" at that very first show. Cowboy also is responsible for the word 'hip-hop', based on a routine he did at a show the Black Door in 1975-1976 which had become basically public domain by the time that Wonder Mike used it in 'Rapper's Delight' ('I said a hip hop/ Hippie to the hippie/ The hip, hip a hop, and you don't stop' etc). Essentially, Cowboy was a dexterous verbal performer who could freestyle rhymes on the spot; this was a very entertaining thing to watch, and Grandmaster Flash correctly thought that it would be a good accompaniment to his DJing, so he got Cowboy and then two other MCs (Melle Mel and Kid Creole), who because the Three MCs.
Grandmaster Flash and the Three MCs (over the next few years, two more MCs would be added to what would become the Furious Five) basically invented the hip-hop MC as we know it. For starters, they were the first MCs to actually have written rhymes with rhyme schemes. Before the Three MCs, the entertainment that accompanied the music was usually provided by b-boy dancers - the role of the MC was largely functional, not entertaining. However, the prominence of Grandmaster Flash and his innovative, technically proficient DJing - which was widely imitated - meant that the MC increasingly became a prominent part of hip-hop.
So where did Cowboy get his rhymes? Did he just invent it out of whole cloth? Well...there was this thing called 'the dozens' in African-American culture - basically, extended, rhymed 'yo mama' jokes.
A recent book by Elijah Wald, Talkin' Bout Your Mama, discusses 'the dozens' at length. You can get a sense of what 'the dozens' sounds like on this 1959 Bo Diddley track, 'Say Man', which features Diddley and his guitarist trying to insult each other, dozens-style, for fun and profit. The dozens is a very obvious precursor for rap - it's African-American people rhyming, often in a somewhat musical way.
It's also important to recognise that other forms of spoken word poetry have directly influenced the idea of rapping over music, beyond the 'dozens' tradition. What with the large influence of Jamaican sound system culture in the way that hip-hop has developed - Kool Herc and all - sound system culture also has a tradition of 'toasting', where DJs chant in a monotone over the music; as some early hip-hop DJs in New York in the 70s had Jamaican roots, there is an obvious influence of toasting on rap which is separate to the American tradition of the dozens.
Similarly, there is also an obvious influence of spoken word poetry, and in particular the quite literary traditions of jazz poetry and beat poetry, on rap. This is most obviously personified in the 1971 track 'The Revolution Will Not Be Televised' by Gil Scott-Heron, and his contemporaries the Last Poets. It doesn't quite have the rhythmicness of rap, but Gil Scott-Heron comes from a more literary tradition involving Langston Hughes's 'jazz poetry' (e.g., this 1958 performance of 'The Weary Blues', where Hughes recites poetry over jazz) or perhaps Jack Kerouac's experiments with beat poetry over music.
If you look at 'Rapper's Delight' by the Sugarhill Gang (often considered the first hip-hop single), it has the Big Bank Hank verse dissing Superman which is obviously influenced by the dozens:
I said he's a fairy I do suppose
Flying through the air in pantyhose
He may be very sexy or even cute
But he looks like a sucker in a blue and red suit
I said you need a man who's got finesse
And his whole name across his chest
He may be able to fly all through the night
But can he rock a party 'til the early light
He can't satisfy you with his little worm
But I can bust you out with my super sperm
However, in contrast, the social message in Grandmaster Flash and the Furious Five's 'The Message' comes from a more obviously jazz poetry/Gil Scott-Heron/Last Poets-esque kind of place. For instance, Duke Bootee, who wrote the lyrics of 'The Message' and rapped on much of it, has a website where he proudly quotes a journalist's comparison of his rapping to Langston Hughes.