Federation is (at least by TNG) a post-scarity society. You don't have to work, an yes, I assume that there a lot of people who don't. Most still want to do something they feel makes a positive contiribution to the society.
Many famous scientists, politicians and explorers of the past were from nobility or other wealthy upper classes. These were people who didn't have to work, they already had all the wealth they needed. They did those things because they wanted to, not because they had to. It's like that for everyone in Trek.