Can your school take your invention from you?

If some sort of agreement was signed at all to build the invention, the most likely yes. If school materials were used to build the invention, yes. Applying for a patent is a way to protect the invention.