Although the Buddhist scripture was originally written in ancient Pali and Sanskrit, the Buddha didn’t speak either of those.
Shakyamuni Buddha (the historical Buddha), spoke a local dialect called Magadhi or Ardhamagadhi. There is no record of the Buddha’s words in his own language. This makes knowing exactly what the Buddha said very difficult.
However, as Ven. Thich Nhat Hanh explains, we can still find a way to ensure a teaching is correct:
By comparing the two extant sutra recensions, we can see which teachings must have preceded Buddhism’s dividing into schools. When the sutras of both transmissions are the same, we can conclude that what they say must have been there before the division. When the recensions are different, we can surmise that one or both might be incorrect. The Northern transmission preserved some discourses better, and the Southern transmission preserved others better. That is the advantage of having two transmissions to compare.
(Note: Southern Transmission refers to Theravada, and Northern Transmission refers to Mahayana)
Prakrit isn’t a language, but a category of languages. It refers to any “demotic” natural language or dialect (i.e. the languages of everyday folk) as opposed to Sanskrit (the “perfected” language of the scriptures. The word “sanskrit” is from the same root as “samskrta,” meaning “assembled”. So the Buddha did speak a Prakrit.