Copyright
According to the Berne Convention, certain works (mainly artistic and literary, but also scientific - including computer programs) have the copyright assured since their creation. Regardless of whether you have made any publication or formal registration of it or not.
The first rule is that no formality is required to obtain protection. For countries such as Brazil, where the work is expected to be registered, this is only ad probandum tantum, and completely optional. Thus, the result of this principle is that - contrary to what happens, for example, with patents - the exclusive right arises from the creation and not from any state declaration, and is guaranteed without any other formality.
Registering
Like pointed out by sergiopereira, anyone can take your program and say that it was he who did it. It is necessary then prove that you are the true author. The most guaranteed way is through an official record - in the case of Brazil, by the INPI. Theoretically, the procedure is relatively simple and inexpensive:
In recent years, computer program registration is getting simpler and source code can be sent in PDF format.
The application for software registration, with up to five envelopes, costs R $ 590, falling to R $ 235 in the case of natural people; micro-enterprises, small businesses and cooperatives so defined in Law; educational and research institutions; non-profit entities, as well as public bodies.
But in practice, as we know, a computer program is not "ready": it is constantly evolving. When, and how often, one should make this record, this is something only a lawyer can tell you (I myself have had this doubt for years, and still do not know for sure).
Alternatives?
If the registration is optional - being its only (?) utility prove the authorship in the case of a lawsuit - one can seek alternatives to it. Again, only a lawyer can tell you what it serves and what doesn’t, but my impression particular is that if you do a very public and open disclosure - host your code on Github, for example - it’s easier to prove that it’s really yours.
Licensing
Assuming you are the author of a program - and have not produced the same as an employee of a company or similar situation (in which case the rights are from your contractor, not yours) - you own a "monopoly" over who can copy your program. You could sell/transfer the rights on it, or - what is more common - license it so that others have the right to: a) use it; b) study it; c) modify it; d) redistribute it. The programs "owners" usually only give the right to use it, but currently is growing the number of projects that also allow you to study it.
Open Source or Free Software?
Although the terms "open source" (open source) and "free software" (free software) are used as synonyms, they do not mean exactly the same thing: the code is opened when the person who receives the binary (if any) also receives/has the right to receive the source code. What she can do with that code, that depends on the case...
The concept of free software is also subject to interpretation, but in general it is a program whose user has the right to study it and modify it (which implies the need for distribution of sources as well) and redistribute it to whomever he wants, without the need for additional permission from its author and/or payment of royalties. Some forms of licensing (copyleft) require any derivative work - modifications you’ve made to the program, or other programs that include and interact with the original - be licensed in the same way or in a very similar way. Others (copyfree) do not impose this restriction. Which of the two is "freer" is the subject of much controversy...
Then answering the question...
If you want to develop free software, the first thing to do is to ensure that the rights of this software are yours (not to develop at someone’s service) and to plan to prove that they are yours. Decide how important it is to prevent someone from "stealing" them, and act accordingly (publishing it right away, registering it right away, keeping it secret until you have enough "volume" for the cost of the registration to be worth, etc).
Then think about how "free" you want your program to be: just credit it to you? Use something like licenses BSD or MIT (or Apache, but in my opinion it is complicated beyond necessary). Want to ensure that no one "takes advantage" of your program without "playing under the same rules of the game"? Use GNU GPL or - in the case of programs on the server side - GNU Affero GPL. Something in the way? A GNU LGPL for example allows your program to be linked with non-free programs, but still ensures that your program itself remains free.
(and if you don’t care how they use your show, WTFPL is always an option... : P)
You can always use another license, of course, or even write your own license in its specific terms (see a lawyer; and don’t copy a pre-existing license and use it as your own - license texts also are protected by copyright...). But if you can avoid, don’t do that: software doesn’t exist "on an island" - it’s always necessary/interesting to integrate program A with B, embed A in B, or extend it to other purposes. The more "standardized" the license you use, the greater the chance that other people can use it without worry - and that’s what’s important about free software: one collaborator from here, one from there, and together we can do something that individually we would never be able to do.
To enrich the answer, here is a link that can help you choose the license for your software: http://choosealicense.com/licenses/
– gmsantos