Listen to previous freestyle flows and battles by great artists (e.g., rappers like Jin, Jay-Z, Yusaf, Benefit, Rakim, Big L, Eminem, and any other great artist that spits hardcore rap).
Understand the techniques those artists use to flow and battle, which will help you enhance those techniques yourself.
Start writing rhymes. Write down anything that comes to mind and try to rhyme it. Using your emotions is a good way to describe what you're feeling when you spit or write lyrics. Make sure you eat a hearty meal before attempting a battle.
Practice free-styling -- anytime, anywhere, as much as you can. Even if you run out of things to freestyle about, just continue spitting, no matter how wack you think you sound. It helps you develop better rhymes and your mind becomes more focused on what sounds good when you spit. It's like a mental workout. So always practice spitting anywhere.
Once you've noticed you can spit on spot (when you want to), try to spit about more specific things. Direct your raps toward things that bother you or upset you. Anything you dislike or want to talk about, try to spit about it. Once again, practice this until you feel you've got it down.
Start freestyle battling. The first step to freestyle battling is to practice the first 5 techniques in a battle against a friend or someone who it wouldn't matter to if you messed up. Constantly battle like that with people, especially if you can find a friend who is actually good at battling so they can teach how to improve what you lack. Again, continue to practice this until other friends you know (especially those into hip-hop music) think you're pretty good.
Have your first real battle against someone you at least somewhat dislike. If you can find someone who just gets you emotional or who angers you, it makes it easier to flow about them. You want to make sure when you flow about them you include 3 major things.
Metaphors - Making comparisons with your target (the person you're battling) to something that denigrates them.
Disses - Saying things that either make fun of them in general (e.g., how they dress, speak, spit, look, walk, talk, act, or their personality) or about them personally (e.g., the way they live, their past, their lifestyle, weaknesses about them, anything that directly goes against them in a way that makes fun of them).
Punch-Lines - a Punch-Line basically is a bar (2 lines you spit) that incorporates a Metaphor, Dis, and/or anything else to enhance the flow directed at your opponent.
Don't worry if you lose your first few real battles, the point is to constantly practice spitting. Continue practicing until you've got it down. And pay attention to how other people spit whom the crowd/judges enjoy. There are many techniques to battling, but these are just the basics.