The first—and most well-known—amendment of the Constitution reads:
Congress shall make no law respecting an establishment of religion, or prohibiting the free exercise thereof; or abridging the freedom of speech, or of the press; or the right of the people peaceably to assemble, and to petition the Government for a redress of grievances.
This means that:
James Madison was instrumental in drafting and advocating for both the ratification of the Constitution and the U.S. Bill of Rights. He is one of the Founding Fathers and is also nicknamed "the father of the Constitution." While he is the one who wrote the Bill of Rights, and thus the First Amendment, he wasn't alone in coming up with these ideas, nor did they happen overnight.
Some important facts to know about James Madison are that even though he was born into a well-established family, he worked and studied his way into political circles. He became known among his contemporaries as "the best informed man of any point in debate."
He was one of the early supporters of the resistance to British rule, which probably later reflected in the inclusion of the right to assembly in the First Amendment.
In the 1770s and 1780s, Madison held positions on different levels of Virginia's government and was a known supporter of the separation of church and state, also now included in the First Amendment.
Even though he is the key person behind the Bill of Rights, when Madison was advocating for the new Constitution, he was against any amendments to it. On one hand, he did not believe that the federal government would ever become powerful enough to need any. And at the same time, he was convinced that establishing certain laws and liberties would allow the government to exclude the ones not explicitly mentioned.
However, during his 1789 campaign to get elected into the Congress, in efforts to win his opposition—the anti-federalists—he finally promised he would advocate for adding amendments to the Constitution. When he was then elected into Congress, he followed through with his promise.
At the same time, Madison was very close with Thomas Jefferson who was a strong proponent of civil liberties and many other aspects that are now part of the Bill of Rights. It is widely believed that Jefferson influenced Madison's views regarding this topic.
Jefferson frequently gave Madison recommendations for political reading, especially from European Enlightenment thinkers such as John Locke and Cesare Beccaria. When Madison was drafting the Amendments, it is likely that it wasn't solely because he was keeping his campaign promise, but he probably already believed in the need to protect individual liberties against the federal and state legislatures.
When in 1789, he outlined 12 amendments, it was after reviewing over 200 ideas proposed by different state conventions. Out of these, ultimately 10 were selected, edited, and finally accepted as the Bill of Rights.
As one can see, there are many factors that played into the drafting and ratification of the Bill of Rights. The anti-federalists, along with Jefferson's influence, states' proposals, and Madison's changing beliefs all contributed to the final version of the Bill of Rights. On an even larger scale, the Bill of Rights built on the Virginia Declaration of Rights, the English Bill of Rights, and the Magna Carta.
Similarly to the entire Bill of Rights, the language of the First Amendment comes from a variety of sources.
As mentioned above, Madison was a proponent of the separation of church and state, and this is probably what translated into the first part of the Amendment. We also know that Jefferson—Madison's influence—was a strong believer of a person having the right to choose their faith, as to him religion was "a matter which [lied] solely between Man and his God."
With regard to the freedom of speech, it is safe to assume that Madison's education along with literary and political interests had a great effect on him. He studied at Princeton where a great focus was placed on speech and debate. He also studied the Greeks, who are known for valuing freedom of speech, too—that was the premise of Socrates' and Plato's work.
In addition, we know that during his political career, especially when promoting the ratification of the Constitution, Madison was a great orator and gave an enormous number of successful speeches. Similar free speech protections written into various state constitutions also inspired the language of the First Amendment.
Besides his call-to-action speeches, Madison's eagerness for spreading ideas about the importance of the new Constitution also reflected in his vast contribution to the Federalist Papers—newspaper-published essays explaining to the general public the details of the Constitution and their relevance.
Madison thus highly valued the importance of the uncensored circulation of ideas. Also the Declaration of Independence defied heavy censorship imposed by the British government and upheld by early governors upheld.
Freedom of Assembly is closely associated with the freedom of speech. In addition, and as mentioned above, Madison's opinions about the need to resist the British rule likely played into inclusion of this freedom into the First Amendment as well.
This right was established by the Magna Carta already in 1215 and was reiterated in the Declaration of Independence when the colonists accused the British monarch of not listening to their grievances.
Overall, even though Madison wasn't the only one to draft the Bill of Rights and the First Amendment, he was unquestionably the most important actor in its coming to existence. One final point, however, that is not to be forgotten, is that, just like most other politicians of the time, despite lobbying for all kinds of freedoms for the people, Madison was also an enslaver, which does somewhat taint his achievements.