Q:

Who should own the intellectual property on a program developed in classes?

I am aware of some teachers that own or collaborate with IT companies, so sometimes the assignments that they provide, to the students, are supposed to solve a specific problem that the company may have.
Although I believe that it is always better to solve a real problem, than to use the same old training problems, is this ethical? 
I know that, probably, the schools have their own intellectual property rules, but who should be the owner of the code? 
Can a teacher use the code freely? Can the student use the code freely? What is your experience on this?

A:

Who should and who does are often different. There are laws and contracts to consider as well as "common law." In my own view, forcing a faculty member to give up all rights to the intellectual property (IP) that he/she develops is just wrong. However the law is the law. Contracts intervene. A program developed in a class for a client is normally done under contract and the client likely owns everything. But you know this going in. 
However, a faculty member can't, morally at least, appropriate a student's work unless contracts are in place. Faculty member should, of course, give proper attribution for anything used and also seek permission if there is something useful to be reused. I don't think it is wise for a professor to assume otherwise. 
One possibly useful suggestion is to ask the students at the beginning of a term to agree that code they write is done so under GPL or similar license. But it needs to be agreed to. 
In general, though, "if you create it, you own it". That is probably not universal, but widely held. 
In situations in which contract or the law do give faculty rights to IP created by students everyone is better served, I think, if the faculty member still respects the more general rights of the student. Just because you can appropriate someone else's work doesn't imply that you should. 
On the other hand, if the professor provides the question and guidance in the solution, he/she may have some IP rights to the product. 
For what it is worth, companies also appropriate the work of employees, but under contract. That is mostly well justified and needed, but there are some variations on it that are also immoral in my view. Preventing a researcher from ever working on the same problems after employment ends should not be allowed in a contract and such should be struck down. But, then, I'm not a lawyer or judge. 

Beyond the narrow question it is possible to think "outside the box." If a faculty member normally publishes work on a website he/she can, with the student's permission, publish a student's work under the student's name. This gives the student some visibility and adds a bit of the professor's reputation to that of the student, providing a boost. 
I've also heard of situations in which a student and prof go into business together based on joint work. 

A:

In the software practicum that I teach, students develop new software for a real customer. My school has a standard software license agreement that the students, customer, and I must sign at the start of the semester. The agreement basically gives ownership to everyone involved. The software becomes, in a sense, open source. The agreement has two separate clauses. One gives more "ownership" to the customer. There is some flexibility; one past customer worked with the university to modify the agreement. Also, students could hire their own lawyers to represent them. But I have yet to see that happen.

A:

The 'best' approach is debatable, but you could argue that the easiest approach is to use a permissive license which places few restrictions on who can use the code and what they can do with it. Commercial organisations may have preferences for the particular license you chose, and may be concerned about any obligations that the licence places on them or their future (potentially revenue generating) use of the code. GPL for example is more restrictive about the licensing of derivative works.
Doing nothing (in the absence of any underlying agreements) is unlikely to make the code free-for-all, generally copyright remains with the student (and any other contributors).
A proprietary licence might be acceptable if the project is sufficiently interesting, but is legally more complex to draft.
Regardless of the approach you take, you might want to make sure that if your license affects the student's default rights as the creator of the work, then they are in a position to agree to grant the new license. This in itself ought to be part of the syllabus you're teaching.

