The Federalist No. 10 believed to have been written by James Madison is his commentary regarding his opinions on what he called 'the mischiefs of faction'. In the essay, Madison concludes that there is a 'diversity in the faculties of men' suggesting that a 'pure democracy' made up of the diverse faculties of men might not be in the best interest of the new nation because 'pure demcracy' held too many opinions. Moreover, Madison argued the differences between a democracy and a republic are rooted in the scope of their intended power. His rationale based between the individual citizens with their factional attitudes and local prejudices possibilty corrupting the people (pure democracy) or that of a smaller number of citizens elected by the individual citizens (republic) who have the potential to express a more public view rather than a factional perspective. Madison's perspective rests on the side of a representative government for the new nation, he thought it a more sustainable form of government. However, it must be noted that The Federalist Papers were written with a specific agenda attached to them. Hamilton, Madison, and Jay under the name 'Publius' published the essays in Hamilton's New York newspaper to sway public opinion on the ratification of the U.S. Constitution.
The distinction that Madison makes between the two is that he sees democracy as a danger but sees a republic as the way to prevent that danger from occurring.
When Madison wrote about democracy, he meant a more direct democracy where the people have a large say in the government. He thought that was a bad idea because he didn't think the people were informed enough to make good decisions.
Instead, Madison wanted a republic where there are legislators who represent the people, but who can be kind of independent of them. He thought those legislators would be better informed and would not do stupid things.
So Madison thought that too much democracy was dangerous and he thought a republic would be better because the only people with power would be the people who really knew enough to make good decisions.