Let's say I'm 10ft away from an enemy. In my turn, I choose to move 5ft to get close enough to attack, I do attack, then I use my remaining movement to move out of range of the enemy. Does this still allow them an opportunity attack?
From the PHB, page 195:
[...] You can make an opportunity attack when a hostile creature that you can see moves out of your reach.
While there are certain things that modify this (like the Mobile feat, which I will explain further down), this is the trigger for an opportunity attack. In your example, none of what you did prior to moving out of their reach actually matters. If you were in their reach, and moved out, you provoke an attack of opportunity.
For your specific example, there is a feat for this exact scenario. The Mobile feat, from page 168, gives you, among other benefits, this:
When you make a melee attack against a creature, you don't provoke opportunity attacks from that creature for the rest of the turn, whether you hit it or not.
So if you want to move in, attack, and move away again, you can, but it will require a feat if you don't want to trigger an opportunity attack. Note that moving away might not achieve all that much - if the monster has at least your speed - 5, they can still move after you on their own turn and attack you anyway. You might be better off staying in melee with them. Obviously, there are a number of reasons why this might not be true.