As the wikipedia article clearly states, there are several types of Codeshare Agreements :
Block space codeshare: A commercial (marketing) airline purchases a
fixed number of seats from the administrating (operating/prime)
carrier. A fixed price is typically paid, and the seats are kept away
from the Administrating carrier's own inventory. The marketing airline
decides on their own which booking classes the seats are sold in (the
block of seats are optimised just like another aircraft cabin).
Free flow codeshare: The airlines' inventory and reservation systems
communicate in real-time by messaging, commonly IATA AIRIMP/PADIS
messaging (TTY and EDIFACT). A booking class mapping is defined
between the airlines. No seats are locked to any of the airlines, and
any airline can sell any number of seats.
Capped free flow: Basically the same as above, but a capping (max
number of seats) are defined for each of the marketing airlines
participating in the codeshare with the Administrating
(operating/prime) carrier.
In these cases, profitability of the Airline Marketing the other airline's tickets is possible in any of the following ways:
There might be an agreement with the Carrier Airlines where the Marketing Airline gets to sell the tickets at the same rate as being sold by the Carrier Airline,but the Carrier Airline sells the tickets to the Marketing Airline at lower prices. This helps the Marketing Airline to generate revenue and improve its indicated (apparent) frequency on the given route. The Carrier Airline is benefited in a way where its tickets are sold on more than one portals, so better visibility (Marketing) and hence the name.
Now a days alliances like OneWorld are being formed, where multiple carriers come together and form codeshare bases to mutually benefit each other.
The Marketing Airline might even sell the tickets at a cheaper price than the original Carrier Airline, just to establish its base in the route before starting its operations, so that when it starts its operations, people already know about it. hence, without deploying planes for the route, it can already have an established consumer base.