First, we take full advantage of the Earth's orbital velocity (close to 30 km/sec) by firing in the forward orbital direction.
You set the launch speed: this is the speed relative to Earth. In programming the orbit, we have not taken account of the Earth's own gravitational pull on the spaceship -- this "launch speed" is the speed the ship must have when it's well away from the Earth's field (Note: it's not difficult to compute from this the necessary launch speed from a space station in low Earth orbit. There is a potential hill to climb, but the kinetic energy in the low Earth orbit helps.)
Second: minimizing fuel consumption is essential. The faster the launch speed, the further out the spaceship will go before turning back towards Earth. The basic strategy is to choose the speed so the ship's furthest distance from the Sun is a point on the Martian orbit. Of course, you must choose the launching time so that Mars actually meets the ship at that point!
This orbit is called a Hohmann transfer orbit.
For all the mathematical details, see my lecture Elliptical Orbits: Paths to the Planets